Guest User

Untitled

a guest
May 23rd, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. ## users_controller.rb
  2.  
  3. def edit
  4. @roles = Role.find(:all)
  5. @user = User.find(params[:id])
  6. end
  7.  
  8. def update
  9. @user = User.find(params[:id])
  10.  
  11. respond_to do |format|
  12. if @user.update_attributes(params[:user])
  13. flash[:notice] = "User '#{@user.name}' was successfully updated."
  14. format.html { redirect_to(@user) }
  15. format.xml { head :ok }
  16. else
  17. flash[:error] = "There was a problem updating the user. No users were updated."
  18. render :action => 'edit'
  19. end
  20. end
  21. end
  22.  
  23. ## error (after submitting with bad password confirmation)
  24.  
  25. Showing users/edit.html.erb where line #25 raised:
  26.  
  27. You have a nil object when you didn't expect it!
  28. You might have expected an instance of Array.
  29. The error occurred while evaluating nil.map
  30.  
  31. Extracted source (around line #25):
  32.  
  33. 22: <%= f.password_field :password_confirmation %></p>
  34. 23:
  35. 24: <p><%= label_tag 'role' %><br/>
  36. 25: <%= collection_select(:user, :role_id, @roles, :id, :name) %></p>
  37. 26:
  38. 27:
  39. 28: <p style="margin-top: 20px"><%= f.submit 'Update' %></p>
  40.  
  41. ## edit.html.erb
  42.  
  43. <div class="form">
  44. <h1>Edit user</h1>
  45. <% @user.password = @user.password_confirmation = nil %>
  46.  
  47. <%= error_messages_for :user %>
  48.  
  49. <% form_for(@user) do |f| %>
  50.  
  51. <p><%= f.label 'login' %><br/>
  52. <%= f.text_field :login %></p>
  53.  
  54. <p><%= label_tag 'name' %><br/>
  55. <%= f.text_field :name %></p>
  56.  
  57. <p><%= label_tag 'email' %><br/>
  58. <%= f.text_field :email %></p>
  59.  
  60. <p><%= label_tag 'password' %><br/>
  61. <%= f.password_field :password %></p>
  62.  
  63. <p><%= label_tag 'password_confirmation', 'Confirm Password' %><br/>
  64. <%= f.password_field :password_confirmation %></p>
  65.  
  66. <p><%= label_tag 'role' %><br/>
  67. <%= collection_select(:user, :role_id, @roles, :id, :name) %></p>
  68.  
  69.  
  70. <p style="margin-top: 20px"><%= f.submit 'Update' %></p>
  71. <% end -%>
  72.  
  73. </div>
Add Comment
Please, Sign In to add comment