ruby - Rails trying to create a user when updating user details -
i'm learning rails , building authentication system using guides around web , following railscasts tutorials.
i've come stand still @ moment , need bit of assistance if possible.
when ever try edit user profile, error message tells me can't create account due fields such email , username being taken.
looking around seems it's related how edit form being submitted, can't solve it!
any appreciated.
users_controller.rb
def edit @user = user.find(params[:id]) end def update @user = user.find(params[:id]) if @user.update_attributes(user_params) flash[:success] = "profile updated" redirect_to @user else render 'edit' end end
edit.html.erb <%= form_for :user, url: '/users' |f| %>
<form class="m-t" role="form" action="#"> <div class="form-group"> <%= f.label :name %> <%= f.text_field :name, class: 'form-control', autocomplete: "off" %> </div> <div class="form-group"> <%= f.label :user_type %> <%= f.select(:user_type, ['admin', 'technical', 'accounts'], {}, { :class => 'form-control' }) %> </div> <div class="form-group"> <%= f.label :email %> <%= f.text_field :email, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :telephone %> <%= f.text_field :telephone, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :mobile %> <%= f.text_field :mobile, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :user_name %> <%= f.text_field :user_name, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :password %> <%= f.password_field :password, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :password_confirmation %> <%= f.password_field :password_confirmation, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :company_admin%> <%= f.check_box :company_admin, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :user_admin %> <%= f.check_box :user_admin, class: 'form-control' %> </div> <div class="form-group"> <%= f.label :emergency_contact %> <%= f.check_box :emergency_contact, class: 'form-control' %> </div> <div class="form-group"> <%= f.submit "submit", class: "btn btn-primary block full-width m-b" %> </div> </form> <% end %>
rails log
user exists (0.2ms) select 1 one "users" "users"."email" = 'emailaddress@gmail.com' limit 1 user exists (0.1ms) select 1 one "users" "users"."user_name" = 'ausername' limit 1
user.rb
class user < activerecord::base has_secure_password validates :name, presence: { message: "please enter name." } validates_uniqueness_of :email, presence: { message: "please enter email address." } validates_format_of :email, with: /\a([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z] {2,})\z/i, message: "please enter valid email address.", allow_blank: true validates :telephone, presence: { message: "please enter phone number." } validates :mobile, presence: { message: "please enter mobile number." } validates_uniqueness_of :user_name, presence: { message: "please enter user name." } validates_confirmation_of :password, presence: { message: "please enter password" }, allow_nil: true before_create { generate_token(:auth_token) } def generate_token(column) begin self[column] = securerandom.urlsafe_base64 end while user.exists?(column => self[column]) end def send_password_reset generate_token(:password_reset_token) self.password_reset_sent_at = time.zone.now save! usermailer.password_reset(self).deliver end end
wait i'm wrong on validation. spotted it.
<%= form_for :user, url: '/users' |f| %>
this won't use @user
object, means rails thinks you're trying create user.
switch to
<%= form_for @user |f| %>
rails infer correct place post won't need url option anymore.
Comments
Post a Comment