jquery - Rails validates a field that isn't included in page -
i have sample app created , i'm having trouble editing fields. whenever try , update fields, it's triggers "validates :password_confirmation" in user model though haven't included password_confirmation field in page.
here of codes:
this 'pages/services.html.erb' display data
<h1>shows users</h1> <% if current_user %> <table border="1"> <% @column_names.each |column_name| %> <% if column_name == "name" || column_name == "username" || column_name == "email"%> <td> <strong><%= column_name.capitalize %></strong> </td> <% end %> <% end %> <% @users.each |user| %> <tr> <td><%= user.name%></td> <td><%= user.username%></td> <td><%= user.email%></td> <td><%= link_to "edit", edit_user_path(user)%></td> <td><%= link_to "delete", '#'%></td> </tr> <%end%> </table> <% end %>
and here's 'users/edit.html.erb'. have users model fyi.
edit
<%= form_for @user |f| %> <%= render 'shared/error_message' %> <p> <%= f.text_field :name%><br/> <%= f.label :name %><br/> </p> <p> <%= f.text_field :username %><br/> <%= f.label :username%><br/> </p> <p> <%= f.email_field :email%><br/> <%= f.label :email %><br/> </p> <p> <%= f.submit "update" %></p> <% end%>
and here's userscontroller code:
class userscontroller < applicationcontroller def new @user = user.new @title = "user sign up" end def create @user = user.new(params[:user]) if @user.save sign_in_check @user redirect_to root_path, :flash => { :success => "welcome bakeshop"} else @title = "user sign up" render 'new' end end def edit @user = user.find(params[:id]) end def update @user = user.find(params[:id]) if @user.update_attributes(params[:user]) redirect_to pages_services_path, :notice => "update successful" else render "edit" end end end
can explain phenomenon , suggest fix?
edit
added user.rb file code:
class user < activerecord::base require 'digest/md5' attr_accessible :password_confirmation, :name, :email, :password, :username before_save :encrypt_password validates :name, :presence => true, :length => { :maximum => 25} validates :username, :presence => true, :length => { :maximum => 25}, :uniqueness => {:case_sensitive => false} validates :email, :presence => true, :uniqueness => {:case_sensitive => false} validates :password, :presence => true, :confirmation => true, :length => {:within => 6..40} validates :password_confirmation, :presence => true def self.authenticate(username, password) user = user.find_by_username(username) if user && user.password == digest::md5.hexdigest(password) user else nil end end def encrypt_password self.password = digest::md5.hexdigest(password) end end
i think problem might here:
validates :password, :presence => true, :confirmation => true, #confirm 1 :length => {:within => 6..40} validates :password_confirmation, :presence => true #confirm 2
in first validates
block, code i've marked confirm 1
doing same thing code i've marked confirm 2
. first block need when there's field password, code automatically confirm it.
the second bit causing error because not associated password field explicitly , you're asking rails confirm presence of :password_confirmation
attribute before user created or updated, unnecessary. make sense?
Comments
Post a Comment