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??
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:urland:methodtoform_for. The only thing you need is:Rails takes care of the rest automatically.
The code in the
createmethod of your controller is incorrect. Whencreateis called it is a new request and therefore@talentwill 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 ofTalentand callsaveon it.So, it should look like this:
Or like this: