Repository navigation
Allow users to edit details
Add these to your files
account_controller.rb
#
#Edit User
#
#
#allow a user to edit their details
def edit
@user = User.find(self.current_user.id)
end
#
#update the user table
def update
@user = User.find(self.current_user.id)
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
redirect_to :action => 'index'
else
render :action => 'edit'
end
end
create the following views
_userForm.rhtml
<%= error_messages_for 'user' %> <!--[form:user]--> <!-- all custom fields here --> <p><label for="user_email">Email Address</label><br/> <%= text_field 'user', 'email' %></p> <!--[eoform:user]-->
edit.rhtml
<p>Edit your details</p>
<%= start_form_tag :action => 'update' %>
<%= render :partial => 'userForm' %>
<%= submit_tag 'Edit' %>
<%= end_form_tag %>
Is this a really bad way to do this? What is to stop someone from building a request that will reset the users password or change their login?
:: It should be safe if the model is protected with attr_protected or attr_accessible
And if they update their email, you should go through the activate process again if you’ve enabled this feature.
I was a bit confused about using attr_protected. After looking it up, it seems if you want to make sure they can’t change their login or other info you add attr_protected to the user model. This can be done with the line:
- Protects from url auto assignment
attr_protected :login
Read more about attr_protected, here:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001005
but adding that causes a ton of tests to fail, it seems to mess up the default signup function. I worked around many of these issues only to find new problems created by adding the attr_protected on login. Such as the default tests all failing.
Anyone else have better suggestions
The defaults test all assume that none of the attributes are protected. You’ll need to edit them to make sure that :login isn’t being auto-assigned. When you need to assign :login from a form, don’t use User#create, do it like this:
@user = User.new(params[:user])
@user.login = params[:user][:login]
@user.save
You’ll want to restrict this kind of action to, say, Admins who have the right to change logins.