【问题标题】:NoMethodError in Person#newPerson#new 中的 NoMethodError
【发布时间】:2014-03-21 07:03:12
【问题描述】:

# 的未定义方法 `people_path' 提取的源代码(第 4 行附近):

<h1>Person#new</h1>
<p>Find me in app/views/person/new.html.erb</p> 

<% form_for (@people) do |f|%>
<%= f.label :first_name%>
<%= f.text_field :first_name%>
<%= f.label :last_name%>
<%= f.text_field :last_name%>
<%= f.label :company%>
<%= f.text_field :company%>
<%= f.label :email%>
<%= f.text_field :email%>
<%= f.label :phone%>
<%= f.text_field :phone%>
<%end%>

---- 人物控制器 ----

  def new
    @people = Person.new
  end

我打电话给 person/new 时出错

----路线-----
获取“人/新”
获取“人/索引”
获取“人/节目”
获取“人/删除”
获取“人/更新”
获取“人/创建”

资源:人

----迁移----
类 CreatePeople 默认更改
create_table :人们做|t|
t.string :first_name
t.string :last_name
t.string :公司
t.string :电子邮件
t.string :电话
t.timestamps
结束
结束
结束

--型号--
类人 结束

【问题讨论】:

  • 我没有看到共享代码中调用了任何each 方法。请分享相关错误的代码。
  • 我在做一个假设,但是如果您将@people 更改为@person 会怎样?检查people_controller.rb 文件,在new 操作下查看对象应该是什么。
  • 我的控制器是 person_controller,在新动作中有这个 def new @people = Person.new end
  • 您是否拥有 config.rb 中的资源:persons?您的 personController 中是否有 def new end merhod ?
  • 此刻放上“路线”

标签: ruby-on-rails ruby


【解决方案1】:

routes.rb 中,您的路线应定义如下:

resources :people

这样people_path 将可用。 之后,您可以运行rake routes 并检查为您创建的路由,如下所示:

people      GET    /people(.:format)                     people#index
            POST   /people(.:format)                            people#create
new_person  GET    /people/new(.:format)             people#new
edit_person GET    /people/:id/edit(.:format)       people#edit
person      GET    /people/:id(.:format)                 people#show
            PATCH  /people/:id(.:format)                        people#update
            PUT    /people/:id(.:format)                        people#update
            DELETE /people/:id(.:format)                        people#destroy

【讨论】:

  • 我做不到,我没有一个叫做 people 的控制器。我有一个名为 person 的控制器
  • 您应该将您的控制器重命名为people。根据 Rails 约定,如果 Person 是您的模型,那么其复数形式 People 应该是控制器。
【解决方案2】:

尝试替换这个:

<% form_for (@people) do |f|%> 

为此:

<% form_for url_for(controller: 'person', action: 'create', id: @people) do |f|%>

【讨论】:

  • 我做到了,但没有显示字段,并且这样做 并显示字段,但最好放
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多