导轨形式 101
这是我的控制器:
class TalentController < ApplicationController
def index
end
def new
@talent = Talent.new
end
def create
@talent.update_attributes!(params)
end
end
这是我的 app/views/talent/new.html.haml:
= "Create new talent"
= form_for @talent, :url => {:action=>:create, :controller=>"talent"}, :method => :post do |f|
= f.label :First_Name
= f.text_field :first_name
= f.label :Last_Name
= f.text_field :last_name
= f.label :City
= f.text_field :city
= f.label :State
= f.text_field :state
= f.label :Zip_code
= f.text_field :zip_code
= f.submit "Create"
当我点击创建按钮时,我收到此错误。
No route matches [POST] "/talent/new"
这是我的耙子路线:
/ {:action=>"index", :controller=>"talent"}
talent_index GET /talent(.:format) {:action=>"index", :controller=>"talent"}
POST /talent(.:format) {:action=>"create", :controller=>"talent"}
new_talent GET /talent/new(.:format) {:action=>"new", :controller=>"talent"}
edit_talent GET /talent/:id/edit(.:format) {:action=>"edit", :controller=>"talent"}
talent GET /talent/:id(.:format) {:action=>"show", :controller=>"talent"}
PUT /talent/:id(.:format) {:action=>"update", :controller=>"talent"}
DELETE /talent/:id(.:format) {:action=>"destroy", :controller=>"talent"}
我在这里错过了什么?
here's my controller:
class TalentController < ApplicationController
def index
end
def new
@talent = Talent.new
end
def create
@talent.update_attributes!(params)
end
end
here's my app/views/talent/new.html.haml:
= "Create new talent"
= form_for @talent, :url => {:action=>:create, :controller=>"talent"}, :method => :post do |f|
= f.label :First_Name
= f.text_field :first_name
= f.label :Last_Name
= f.text_field :last_name
= f.label :City
= f.text_field :city
= f.label :State
= f.text_field :state
= f.label :Zip_code
= f.text_field :zip_code
= f.submit "Create"
When I hit the create button, I get this error.
No route matches [POST] "/talent/new"
here's my rake routes:
/ {:action=>"index", :controller=>"talent"}
talent_index GET /talent(.:format) {:action=>"index", :controller=>"talent"}
POST /talent(.:format) {:action=>"create", :controller=>"talent"}
new_talent GET /talent/new(.:format) {:action=>"new", :controller=>"talent"}
edit_talent GET /talent/:id/edit(.:format) {:action=>"edit", :controller=>"talent"}
talent GET /talent/:id(.:format) {:action=>"show", :controller=>"talent"}
PUT /talent/:id(.:format) {:action=>"update", :controller=>"talent"}
DELETE /talent/:id(.:format) {:action=>"destroy", :controller=>"talent"}
What did I miss here??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么表单试图发布到
new
。但是,您的代码还需要进行一些改进。首先,没有必要将:url
和:method
传递给form_for
。您唯一需要的是:Rails 会自动处理其余的事情。
控制器的
create
方法中的代码不正确。当调用create
时,它是一个新请求,因此@talent
将是未定义的。你必须先设置它。第二个错误是您使用了update_attributes
,它用于更新数据库中的现有记录。您想要创建一条新记录,因此必须使用create
。另一种方法是简单地创建一个新的Talent
实例并对其调用save
。所以,它应该看起来像这样:
或者像这样:
I don't know why the form is trying to post to
new
. However, there are some improvements to be made to your code. First of all it is not necessary to pass:url
and:method
toform_for
. The only thing you need is:Rails takes care of the rest automatically.
The code in the
create
method of your controller is incorrect. Whencreate
is called it is a new request and therefore@talent
will be undefined. You have to set it first. The second mistake is that you are usingupdate_attributes
, which is used to update an existing record in the database. You want to create a new record, so you have to usecreate
. Another way would be to simply create a new instance ofTalent
and callsave
on it.So, it should look like this:
Or like this: