0

I am trying to reset the new password using Rails 3.When i insert new password(i.e.123456789) and clicked on submit button it gave me the following error.

Error:

NoMethodError in HomesController#resubmitpass

undefined method `stringify_keys' for "123456789":String
Rails.root: C:/Site/library_management1

Application Trace | Framework Trace | Full Trace
app/controllers/homes_controller.rb:179:in `resubmitpass'

Please check my below codes and let me to know how it will be solved ?

views/homes/resetpass.html.erb

<center>
    <%= form_for :users,:url => {:action =>'resubmitpass',:id =>params[:id] } do |f| %>
<div class="pass-div">
<p>
    <%= f.password_field :password,:id => "pre-pass",placeholder:"Enter your new password" %>
</p>
<p>
<%= f.password_field :password_confirmation ,:id => "pre-pass", placeholder:"Enter your password again" %>
</p>
<p>
    <%= f.submit 'Update',:class => "btn btn-success" %>
</p>
</div>
<% end %>
</center>

controller/homes_controller.rb

class HomesController < ApplicationController
     before_filter :authenticate_admin!,only: [:admin]
    def index

    end
    def admin

    end
    def managebooks
        @books=Book.new
        if params[:id]
            @books=Book.find(params[:id])
            @book=Book.all
        end
    end
    def savebooks
        @books=Book.new(params[:books])
        if @books.save
            flash[:notice]="Data has submitted successfully"
            flash[:color]="valid"
            redirect_to :action => 'managebooks',:id => @books.id
        else
            flash[:notice]="Data couldnot submitted successfully"
            flash[:color]="invalid"
            render 'managebooks'
        end
    end
    def remove
        @books=Book.find(params[:id])
        @books.destroy
    end
    def books

    end
    def showbooks
        @books=Book.all
    end
    def searchbooks
        @books=Book.all
    end
    def member
        @users=User.new
    end
    def registration
        @users=User.new
    end
    def savedata
        @users=User.new(params[:users])
        if @users.save
            flash[:notice]="Data has submitted successfully"
            flash[:color]="valid"
            redirect_to :action => 'member'
        else
            flash[:notice]="Data could not submitted successfully"
            flash[:color]="invalid"
            render 'registration'
        end
    end
    def issuebooks
        @issues=Issue.new
    end
    def savedissuebooks
        @issues=Issue.new(params[:issues])
        if @issues.save
            flash[:notice]="information has saved successfully"
            flash[:color]="valid"
            redirect_to :action => 'member'
        else
            flash[:notice]="Data couldnot saved"
            flash[:color]="invalid"
            render 'issuebooks'
        end
    end
    def availablebooks

        @books=Book.all
    end
    def userissues
        @issues=Issue.all
    end
    def magazine
        @magazines=Magazine.new
    end
    def savemagazines
        @users=User.find(params[:id])
        @magazines=Magazine.new(params[:magazines])
        @magazines.user_id=@users.id
        if @magazines.save
            flash[:notice]="Data submitted successfully"
            flash[:color]="valid"
            redirect_to :action => "member"
        else
            flash[:notice]="Data could not saved"
            flash[:color]="invalid"
            render 'magazines'
        end
    end
    def magazineissue
        @magazines=Magazine.all
        @users=User.find @magazines.first.user_id
    end
    def blog
        @blogs=Blog.new
    end
    def savecomments
        @users=User.find(params[:id])
        @blogs=Blog.new(params[:blogs])
        @blogs.user_id=@users.id
        if @blogs.save
            flash[:notice]="Comment has been posted successfully"
            flash[:color]="valid"
            redirect_to :action => "showcomment"
        else
            flash[:notice]="Comment could not saved"
            flash[:color]="invalid"
            render 'blog'
        end
    end
    def showcomment
        @blogs=Blog.all
    end
    def newspaper
        @newspapers=Newspaper.new
    end
    def savenewspaper
        @users=User.find(params[:id])
        @newspapers=Newspaper.new(params[:newspapers])
        @newspapers.user_id=@users.id
        if @newspapers.save
            flash[:notice]="newspaper data saved successfully"
            flash[:color]="valid"
            redirect_to :action => "member"
        else
            flash[:alert]="Data could not saved successfully"
            flash[:color]="invalid"
            render 'newspaper'
        end
    end
    def adminnewspaperissue
        @newspapers=Newspaper.all
        @users=User.find @newspapers.first.user_id
    end
    def userprofile
        @users=User.find(params[:id])
    end
    def updatedata
        @users=User.find(params[:id])
        if @users.update_attributes(params[:users])
            flash[:notice]="User Data has updated"
            flash[:color]="valid"
            redirect_to :action => 'member'
        else
            flash[:alert]="Data could not updated"
            flash[:color]="invalid"
            render  'userprofile'
        end
    end
    def forgetpass
        @users=User.new
    end
    def sendemail
        @users=User.find_by_email(params[:users][:email])
        if @users.email==params[:users][:email]
            UserMailer.registration_confirmation(@users).deliver
            flash[:notice]="check your mail to reset your password"
            flash[:color]="valid"
            redirect_to :action => "checkmail"
            else
            flash[:alert]="You might entered wrong email address"
            flash[:color]="invalid"
            render 'forgetpass'
            end  
    end
    def resetpass
        @users=User.new
    end
    def resubmitpass
        @users=User.find(params[:id])
        if @users.update_attributes(params[:users][:password])
            flash[:notice]="Your password updated succesfully"
            flash[:color]="valid"
            redirect_to :action => "member"
        else
            flash[:alert]="Your password could not updated successfully"
            flash[:color]="invalid"
            render 'resetpass'
        end
    end
end

model/user.rb

class User < ActiveRecord::Base
  attr_accessible :address, :email, :first_name, :last_name, :password, :password_hash, :password_salt, :tel_no ,:password_confirmation
  attr_accessor :password
  before_save :encrypt_password
 EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
 validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
 validates :first_name, :presence => true, :length => {:in => 3..10}
 validates :last_name , :presence => true , :length => {:in => 3..10}
 validates :tel_no , :presence => true , :length => {:in => 1..10}
 validates :password, :confirmation => true
 validates_length_of :password, :in => 6..20, :on => :create
 def self.authenticate(email, password)
    user = find_by_email(email)
    if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
      user
    else
      nil
    end
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end
  has_many :issue
  has_many :book
  has_many :magazine
  has_many :blog
  has_many :newspaper
end

db/migrate/20150311112733_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :email
      t.string :password
      t.string :tel_no
      t.string :address
      t.string :password_hash
      t.string :password_salt
      t.timestamps
    end
  end
end

Actually the error is coming in this line "if @users.update_attributes(params[:users][:password])".Please help me to resolve this error.

rajat_474
  • 346
  • 1
  • 4
  • 15

1 Answers1

0

It is happening because you are using update_attributes incorrectly.

You need to specifically pass the params to the associated column like update_attributes(:password => params[:password]) or better use Object.update_attribute(:only_one_field, "Some Value") If you want to pass the whole params hash you can simply use this: update_attributes(params[:user]).

You can read more about it here.

Community
  • 1
  • 1
harshs08
  • 700
  • 10
  • 29