2012-01-12 97 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