Note that there are some explanatory texts on larger screens.

plurals
  1. PORails update_attributes using has_secure_password
    primarykey
    data
    text
    <p>I am working in a project where users can either have <code>admin: true / false</code>. The application will only let admin users login in to the system, the other users are just clients whose settings only admins can edit. This is some sort of commerce application.(<strong>Rails 3.2.13</strong>)</p> <p>I would like to keep both concepts in the same table since in the future there will possibly be the option of logging in for non-admin users and interact with their profiles, so all the logic will be already implemented.</p> <p>I've got this <code>User</code> resource:</p> <pre class="lang-rb prettyprint-override"><code>ActiveRecord::Schema.define(:version =&gt; 20131204200554) do create_table "users", :force =&gt; true do |t| t.string "name" t.string "surname" t.boolean "admin" t.string "password_digest" t.string "email" t.string "auth_token" end end </code></pre> <p>This is the user model:</p> <pre class="lang-rb prettyprint-override"><code>class User &lt; ActiveRecord::Base has_secure_password attr_accessible :name, :surname,:email, :password, :password_confirmation validates :name, presence:true, length: { maximum:50} validates :first_surname, presence:true, length: { maximum:50} VALID_EMAIL_REGEX= /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true,format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive:false} validates :password, length:{minimum:6} validates :password_confirmation, presence:true before_save{ self.email.downcase! } before_save :generate_auth_token private def generate_auth_token self.auth_token=SecureRandom.urlsafe_base64 end end </code></pre> <p>And I am trying to implement the functionality of <code>User</code> editing, however I only want to allow the editing of <code>name</code>, <code>surname</code> and <code>email</code>. Hence, I present only those fields in the form:</p> <pre class="lang-rb prettyprint-override"><code>&lt;%= form_for(@user) do |f| %&gt; &lt;%= f.label :name,"Name" %&gt; &lt;%= f.text_field :name %&gt; &lt;%= f.label :surname,"Surname" %&gt; &lt;%= f.text_field :surname %&gt; &lt;%= f.label :email,"Email" %&gt; &lt;%= f.text_field :email %&gt; &lt;%= f.submit "Save" %&gt; </code></pre> <p>I am trying to accomplish the goal with this code:</p> <pre class="lang-rb prettyprint-override"><code>def update @user = User.find(params[:id]) if @user.update_attributes(params[:user]) # Handle a successful update. flash[:success]="User updated successfully" redirect_to @user else flash[:danger]="Error updating user" render 'edit' end end </code></pre> <p>My problem is that when trying to <code>update_attributes</code> I get <em>not</em> unexpectedly an error validating <code>password/password_confirmation</code>, but since I'm using <code>has_secure_password</code>, these fields do not exist in the database, only <code>password_digest</code>. I am thinking about the best option to accomplish all this:</p> <ol> <li>Update <code>@user</code> object with new field values.</li> <li>Reflect this change in <code>User</code> table.</li> <li>Run the validations, i.e. email validation.</li> </ol> <p>While using <code>has_secure_password</code>. These are the options so far:</p> <ol> <li>Use of <code>update_attribute</code>(best so far).<br/> <strong>1.1 Pro</strong>: updates the <code>User</code> table<br/> <strong>1.2 Con</strong>: I have to <code>update_attribute(field1)</code> for each of the fields, so more lines of code<br/> <strong>1.3 Con</strong>: Apparently this method no longer exists in Rails 4, problem in case an upgrade is desirable in the future.<br/> <strong>1.4 Con</strong>: No validations<br/></li> <li>Use of <code>@user.attributes=params[:user]</code> in the controller method<br/> <strong>2.1 Pro</strong>: Updates multiple fields at once.<br/> <strong>2.2 Con</strong>: Does not update the <code>User</code> table.<br/></li> <li>User of <code>update_attributes</code><br/> <strong>3.1 Pro</strong>: Both multiple fields and table update<br/> <strong>3.2 Con</strong>: Not working ( duh!)<br/></li> </ol> <p>Suggestions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload