2012-01-12 101 views
0

我正在嘗試使用複選框在設置頁面中更新我的個人資料設置。一旦複選框被點擊,我想重定向到設置頁面,所以我添加了一個新的動作到我的控制器更新配置文件,但重定向到設置。不過,我發現了以下錯誤:Form_for錯誤 - 無路線匹配帖子

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

這裏是我的form_for代碼:

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

edit_settings行動,我profiles控制器:

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 

裏面我routes.rb文件:

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

很好的解釋,謝謝!添加'匹配「配置文件/ edit_settings」,:到=>「profiles#edit_settings'到我的路線文件工作。 – tvalent2 2012-01-12 02:12:33