Dre
Dre

Reputation: 151

Rails singular route issue

I have an account/settings page people can visit to update their account. It's a singular resource, so they can (or should) only be able to update their own account settings. I'm running into a weird URL format when there are form errors displayed.

If they are on /account/settings/edit and try to submit the form with errors (not a valid email address, for example) they are redirected to /account/settings.1 where it shows them what went wrong (in our example, not a valid email address).

Everything "works" but I was wondering why there is a .1 being appended to the URL. I figured they would be sent back to account/settings or account/settings/edit where they can correct the error. Am I doing something wrong?

routes.rb

namespace :account do
  resource :settings, :only => [:show, :edit, :update]
end

settings_controller.rb

def edit
  @account = Account.find(session[:account][:id])
end

def update
  @account = Account.find(session[:account][:id])
  if @account.update_attributes(params[:account])
    redirect_to account_settings_path
  else
    render 'edit'
  end
end

rake routes

edit_account_settings  GET  /account/settings/edit(.:format)  account/settings#edit
     account_settings  GET  /account/settings(.:format)       account/settings#show
     account_settings  PUT  /account/settings(.:format)       account/settings#update

Upvotes: 2

Views: 255

Answers (1)

RobHeaton
RobHeaton

Reputation: 1390

Make sure you generate your paths using edit_account_settings_path, NOT edit_account_settings_path(@user). For singular resources you shouldn't pass in a resource, because, as you say, there is only one of them.

Upvotes: 3

Related Questions