tvalent2
tvalent2

Reputation: 5009

Form_for error - no route matches post

I'm attempting to update my profile's settings using checkboxes in a settings page. Once the checkbox is clicked I want to redirect to the settings page, so I added a new action to my controller that updates the profile but redirects to the settings. However, I'm getting the following error:

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

Here's my form_for code:

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

My edit_settings action in my profiles controller:

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

Inside my routes.rb file:

resources :profiles do
  post :edit_settings
end

Inside rake routes:

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

Upvotes: 0

Views: 3425

Answers (1)

fkreusch
fkreusch

Reputation: 1349

You are creating a member action :edit_settings, and member actions under resources require an id. If you check "rake routes" output, you'll see that it gives you "/profiles/:profile_id/edit_settings", and there is the missing :profile_id parameter in there.

You can fix that by changing your form parameters to {:action => "edit_settings", :controller => "profiles", :profile_id => @profile.id}.

In any case, if this controller function is to update the current user profile, and that only (assuming that this controller will not allow to update other users profiles), a singular resource would probably be a better solution (http://guides.rubyonrails.org/routing.html#singular-resources). This way you would not need to pass the :profile_id parameter.

Upvotes: 2

Related Questions