I have a edit form for @users. In there I have a text_field :username
<%= form_for @user, :url => { :action => "update" } do |f| %>
<%= render 'shared/error_messages', :target => f.object %>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
In my User model I use the friendly_id gem, set to :username as well. So my urls look like domain.com/users/:username instead of :id.
has_friendly_id :username
Now in my application layout, I also use @user.username in my navigation to link to the profile.
All works well, EXCEPT if I leave my :username field empty on save. It fails to save cause of validations,
validates :username, :presence => true, :uniqueness => { :case_sensitive => false }, :length => { :maximum => 50 }
and tries to render "edit" again. But to render "edit" we need the username to create the link in the navbar. And apparently it's passed on as username => "", even though it rightfully so failed to save and proper validations are in place.
def update
@user = current_user
if @user.update_attributes(params[:user])
flash[:success] = "Account updated."
redirect_to :back
else
@title = "Edit"
render "edit"
end
end
So I end up with a RoutingError:
No route matches {:action=>"show", :controller=>"users", :id=>#<User id: 6, username: "", persistence_token: "c2f2545659c186131537155347f46f7da5eb0d51b27707e71da...", created_at: "2011-03-14 14:26:48", updated_at: "2011-03-15 01:54:33", email: "test@test.com", crypted_password: "0d6489b1447d278bc4f7c86bab13787f226a10a302b43ec02ff...", password_salt: "Lq2G80iSVeaitB5PDwf", perishable_token: "Tm7Jzyq8QutfaxL3JLZ8", active: true>}