2012-02-18 16 views
1

を形成:レールはここに私のコントローラだ101

class TalentController < ApplicationController 
    def index 
    end 

    def new 
    @talent = Talent.new 
    end 

    def create 
     @talent.update_attributes!(params) 
    end 
end 

は、ここに私のアプリ/ビュー/タレント/ 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"} 

は、私がここで何を見逃したの??

+0

'指定しないようにしてください。単に '= form_for @ talent'です。 –

+0

また、 '@ talent'が定義されていないため、' create'アクションで例外が発生します。 –

答えて

0

フォームがnewに転記しようとしている理由がわかりません。ただし、コードにはいくつか改善があります。まず、:url:methodform_forに渡す必要はありません。必要なのは、

= form_for @talent do |f| 

です。残りの部分は自動的に処理されます。

コントローラのcreateメソッドのコードが正しくありません。 createが呼び出されたときに新しい要求であるため、@talentは未定義です。最初に設定する必要があります。 2番目の間違いは、update_attributesを使用していることです。これは、データベースの既存のレコードを更新するために使用されます。新しいレコードを作成したいので、createを使用する必要があります。もう1つの方法は、Talentという新しいインスタンスを作成し、saveと呼ぶことです。

だから、それは次のようになります。

def create 
    @talent = Talent.create(params[:talent]) 
end 

またはこのような:url`と `::フォームのmethod`を

def create 
    @talent = Talent.new(params[:talent]) 
    if @talent.save 
    # Saved successfully, do a redirect or something... 
    else 
    # Validation failed. Show form again so the user can fix his input. 
    render :action => 'new' 
    end 
end 
関連する問題