2012-01-12 19 views
0

設定ページのチェックボックスを使用してプロフィールの設定を更新しようとしています。チェックボックスをクリックすると、設定ページにリダイレクトしたいので、コントローラに新しいアクションを追加してプロファイルを更新しますが、設定にリダイレクトします。Form_forエラー - 投稿に一致する経路がありません

<%= form_tag({:action => "edit_settings", :controller => "profiles"}, :html => {:multipart => true }) do |f| %> 

profilesコントローラでマイedit_settingsアクション:私のroutes.rbファイル内

def edit_settings 
    @profile = user.profile 
    if @profile.update_attributes(params[:profile]) 
    redirect_to settings_path, :notice => 'Updated user information successfully.' 
    else 
    render :edit 
    end 
end 

`No route matches {:action=>"edit_settings", :controller=>"profiles"}` 

はここに私のform_forコードです:しかし、私は次のエラーを取得しています

resources :profiles do 
    post :edit_settings 
end 

rake routes内部:

profile_edit_settings POST /profiles/:profile_id/edit_settings(.:format)  {:action=>"edit_settings", :controller=>"profiles"} 

答えて

2

あなたはメンバーのアクションを作成している:edit_settings、およびリソースの下のメンバーのアクションはIDが必要です。 "rake routes"の出力をチェックすると、 "/ profiles /:profile_id/edit_settings"と表示され、見つからない:profile_idパラメータがそこにあります。

フォームパラメータを{:action => "edit_settings", :controller => "profiles", :profile_id => @profile.id}に変更することで修正できます。

このコントローラー機能が現在のユーザープロファイルを更新することであり、このコントローラー機能が他のユーザープロファイルを更新できないと仮定した場合のみ、単一のリソースがより良い解決策(http://guides.rubyonrails.org/routing.html#singular-resources) 。このようにして、:profile_idパラメータを渡す必要はありません。

+0

すばらしい説明、ありがとう! 'match" profiles/edit_settings "、:to =" profiles#edit_settings "を自分のルートファイルに追加しました。 – tvalent2

関連する問題