首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在其他两个对象中复制RCV模式后的NoMethodError

在其他两个对象中复制RCV模式后的NoMethodError
EN

Stack Overflow用户
提问于 2015-08-26 13:24:50
回答 2查看 22关注 0票数 0

我第三次在rails应用程序中复制了相同的模式,突然页面加载,但是表单不会提交。这个问题看上去是:

代码语言:javascript
复制
NoMethodError in Transportation#new
Showing /home/cchilders/projects/rails_projects/jared_test/app/views/transportation/new.html.erb where line #12 raised:

undefined method `transportations_path' for #<#<Class:0x007fc3e8013a20>:0x007fc3ee3c0fc8>
Extracted source (around line #12):
10
11
12
13
14
15

<div>
  <div>
    <%= form_for(@transportation) do |f| %>
        <div>
          <%= f.label :name %><br>
          <%= f.text_area :name %>

Rails.root: /home/cchilders/projects/rails_projects/jared_test

在transportation_controller中我们有:

代码语言:javascript
复制
class TransportationController < ApplicationController

    def index
        @transportations = Transportation.all
    end

    def new
        @transportation = Transportation.new
    end

    def create
        @transportation = Transportation.new(transportation_params)
        if @transportation.save
            redirect_to '/vacations'
        else
            render 'new'
        end
    end

    private 
      def transportation_params
          params.require(:transportation).permit(:name)
      end

end

在routes.rb中:

代码语言:javascript
复制
Rails.application.routes.draw do

    get '/vacations'     => 'vacations#index'
    get 'vacations/new'  => 'vacations#new'
    post '/vacations'    => 'vacations#create'

    get '/destinations'     => 'destinations#index'
    get 'destinations/new'  => 'destinations#new'
    post '/destinations'    => 'destinations#create'

    get '/transportation'         => 'transportation#index'
    get 'transportation/new'      => 'transportation#new'
    post '/transportation'        => 'transportation#create'

奇怪的是,我检查了模式与目的地和假期,每个模式都是相同的。我能想象到的唯一不同是模型,但我所要做的只是用一个field...the创建一个对象,另外两个模型要复杂得多,而且保存正确

此问题仅出现在表单提交上,页面将加载。该页如下:

代码语言:javascript
复制
<%= render :template => 'base' %> 


<div>
  <div>
    <h1>Add transportation</h1>
  </div>
</div>

<div>
  <div>
    <%= form_for(@transportation) do |f| %>
        <div>
          <%= f.label :name %><br>
          <%= f.text_area :name %>
        </div>

        <div>
          <%= f.submit "Create" %>
        </div>
    <% end %>

  </div>
</div>

我的运输模式非常简单:

代码语言:javascript
复制
 create_table "transportations", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

此表单要求的URL为“/transportations”,而不是transportations。有人能详细解释一下Rails中的命名是如何工作的吗?我正在通过命名模型Vacation来解决难题,SQL被命名为create table vacations,使得单个对象应该被命名为奇数,但是URL需要复数。用单一模型的CRUDing制作一个基本的rails应用程序的合适模式是什么?

另外,你对老鼠和羊做什么?用sheeps?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-26 13:51:08

只是为了添加关于命名约定的额外输入。这就是为什么当您生成rails generate model vacation时,您将进入数据库vacations表。Rails利用约定而非配置的方法

默认情况下,Active Record使用一些命名约定来了解如何创建模型和数据库表之间的映射。Rails将使类名多元化,以找到相应的数据库表。因此,对于类Book,您应该有一个名为Book的数据库表。

还有一些方法可以覆盖这个约定,如果您有不同的表名,比如数据库中的my_vacantions,您可以按照以下方式将模型绑定到它

代码语言:javascript
复制
class Vacation < ActiveRecord::Base
  self.table_name = "my_vacations"
end

在你问题的最后一部分,猜猜鲁比知道如何正确地转换这类事物,因此这里是你的答案:)羊是羊,老鼠是老鼠。

代码语言:javascript
复制
'sheep'.pluralize            # => "sheep"
票数 1
EN

Stack Overflow用户

发布于 2015-08-26 13:42:18

您应该将rails资源用于路由。如果你想限制他们,你可以

代码语言:javascript
复制
resources :transportations, only: [:new, :create, :index]

当使用form_for(@transportation)时,rails根据对象在正确的位置进行路由。因此,您可以使用资源或显式地设置url。

form_for(@transportation, url: "/transportation")

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32227922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档