Note that there are some explanatory texts on larger screens.

plurals
  1. POAuth issue when changing a column in Users table
    primarykey
    data
    text
    <p>I'm very new to rails and I'm trying to accomplish the following authentication issue:</p> <p>User makes a comment or grants "absolution" (similar to comment) and he gets some coins for it. Coins is the virtual currency in my app and is also a column in the users table.</p> <p>Because of your kind help, I was already capable to update the coins value after writing a comment or grant absolution. However, when I write a comment and log out after that, my login name or password gets changed(?)...I can't login anymore with this account. </p> <p>This is how my User model looks like:</p> <pre><code>require 'digest' class User &lt; ActiveRecord::Base attr_accessor :password attr_accessible :name, :email, :password, :password_confirmation, :twitter_url, :homepage_url, :coins has_many :comments, :dependent =&gt; :destroy has_many :absolutions, :dependent =&gt; :destroy has_many :ratings has_many :rated_sins, :through =&gt; :ratings, :source =&gt; :sins email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i homepage_regex = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix validates :name, :presence =&gt; true, :length =&gt; { :maximum =&gt; 50 } validates :email, :presence =&gt; true, :format =&gt; { :with =&gt; email_regex }, :uniqueness =&gt; { :case_sensitive =&gt; false } validates :twitter_url, :format =&gt; { :with =&gt; homepage_regex } validates :homepage_url, :format =&gt; { :with =&gt; homepage_regex } validates :password, :presence =&gt; true, :confirmation =&gt; true, :length =&gt; { :within =&gt; 6..40 } before_save :encrypt_password def has_password?(submitted_password) encrypted_password == encrypt(submitted_password) end def self.authenticate(email, submitted_password) user = find_by_email(email) return nil if user.nil? return user if user.has_password?(submitted_password) end class &lt;&lt; self def authenticate(email, submitted_password) user = find_by_email(email) (user &amp;&amp; user.has_password?(submitted_password)) ? user : nil end def authenticate_with_salt(id, cookie_salt) user = find_by_id(id) (user &amp;&amp; user.salt == cookie_salt) ? user : nil end end private def encrypt_password self.salt = make_salt if new_record? self.encrypted_password = encrypt(password) end def encrypt(string) secure_hash("#{salt}--#{string}") end def make_salt secure_hash("#{Time.now.utc}--#{password}") end def secure_hash(string) Digest::SHA2.hexdigest(string) end end </code></pre> <p>And this is my comments controller:</p> <pre><code>class CommentsController &lt; ApplicationController before_filter :authenticate, :only =&gt; [:create, :destroy] def new @comment = Comment.new end def create @sin = Sin.find(params[:sin_id]) @comment = current_user.comments.build(params[:comment]) @comment.sin_id = @sin.id if @comment.save flash[:success] = "Comment created! Earned 20 coins." coins_new = current_user.coins.to_i + 20 current_user.update_attribute(:coins, coins_new) redirect_to sin_path(@sin) else flash[:error] = "Comment should have 1 - 1000 chars." redirect_to sin_path(@sin) end end def destroy end private def authenticate deny_access unless signed_in? end end </code></pre> <p>I assume, that it has something to do with the before_save encrypt_password method, but its only a guess. I really appreciate your help and suggestions!</p> <p><strong>Edit:</strong> It gets warmer...It has something to do with the following line in the Comments Controller:</p> <pre><code>current_user.update_attribute(:coins, coins_new) </code></pre> <p>When he updates the :coins column, something seems to go wrong. If you need further info, just drop a comment. Thanks for your help!</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