在Rails 3中定义记住方法以记住我的功能

时间:2015-04-03 11:47:04

标签: ruby ruby-on-rails-3

我想使用我的应用程序登录,但我收到以下错误。

错误:

NoMethodError in SessionsController#loginadmin

undefined method `remember' for #<SessionsController:0x267c300>
Rails.root: C:/Site/swargadwar_admin

Application Trace | Framework Trace | Full Trace
app/controllers/sessions_controller.rb:7:in `loginadmin'

我的代码如下。

的观点/舍/ index.html.erb

<div class="container">
    <div style="text-align:center;"><img src="/assets/admin.png" style="width:100px; height:120px; " /></div>
    <div class="text-div" style="text-align:center;">Swargadwar, Puri Municipality,govt of odisha</div>
            <section>
            <% if !current_user %>              
                <div id="container_demo" >
                    <!-- hidden anchor to stop jump http://www.css3create.com/Astuce-Empecher-le-scroll-avec-l-utilisation-de-target#wrap4  -->
                    <a class="hiddenanchor" id="toregister"></a>
                    <a class="hiddenanchor" id="tologin"></a>
                    <div id="wrapper">
                        <div id="login" class="animate form">
                            <%= form_for :admin,:url => {:action =>'loginadmin',:controller => 'sessions' } do |f| %>
                                <h1>Log in</h1> 
                                <p> 
                                    <label for="username" class="uname" data-icon="u" > Your email or username </label>
                                     <%= f.email_field :email,placeholder:"mysupermail@mail.com",:id => "username" %>
                                </p>
                                <p> 
                                    <label for="password" class="youpasswd" data-icon="p"> Your password </label>
                                     <%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "password" %>
                                </p>
                                <p class="keeplogin"> 
                                    <%= f.check_box :remember_me,:id => "loginkeeping" %>
                                    <label for="loginkeeping">Keep me logged in</label>
                                </p>
                                <p class="login button"> 
                                    <%= f.submit "Login" %>
                                </p>
                                <p class="change_link">
                                    Not a member yet ?
                                    <a href="#toregister" class="to_register">Join us</a>
                                </p>
                                <% end %>
                            </form>
                        </div>

                        <div id="register" class="animate form">
                            <%= form_for :admin,:url => {:action => 'create_registration',:controller => "admins" } do |f| %>
                                <h1> Sign up </h1> 
                                <p> 
                                    <label for="usernamesignup" class="uname" data-icon="u">Your username</label>
                                    <%= f.text_field :user_name,placeholder:"mysuperusername690",:id => "usernamesignup" %>
                                </p>
                                <p> 
                                    <label for="emailsignup" class="youmail" data-icon="e" > Your email</label>
                                     <%= f.email_field :email,placeholder:"mysupermail@mail.com",:id => "emailsignup" %>
                                </p>
                                <p> 
                                    <label for="passwordsignup" class="youpasswd" data-icon="p">Your password </label>
                                    <%= f.password_field :password,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
                                </p>
                                <p> 
                                    <label for="passwordsignup_confirm" class="youpasswd" data-icon="p">Please confirm your password </label>
                                    <%= f.password_field :password_confirmation,placeholder:"eg. X8df!90EO",:id => "passwordsignup" %>
                                </p>
                                <p>
                                     <label for="usernamesignup" class="uname" data-icon="u">Add Image</label>
                                     <%= f.file_field :picture %>
                                </p>
                                <p class="signin button">
                                <%= f.submit "Sign Up"%> 
                                </p>
                                <p class="change_link">  
                                    Already a member ?
                                    <a href="#tologin" class="to_register"> Go and log in </a>
                                </p>
                                <% end %>
                        </div>
                        <div class="error-div">
                                    <% if @admin.errors.any? %>
                                      <div id="error_explanation">
                                      <h2><%= pluralize(@admin.errors.count, "error") %> prohibited this post from being saved:</h2>

                                      <ul>
                                      <% @admin.errors.full_messages.each do |msg| %>
                                     <li><%= msg %></li>
                                     <% end %>
                                     </ul>
                                    </div>
                                  <% end %>
                               </div>
                    </div>
                </div> 
                 <% end %> 
            </section>
        </div>

控制器/ sessions_controller.rb

class SessionsController < ApplicationController
    def loginadmin
        @admin=Admin.authenticate(params[:admin][:email], params[:admin][:password])
        if @admin
            session[:user_id]=@admin.id
            cookies.signed[:user_id]=@admin.id
            params[:admin][:remember_me] == '1' ? remember(@admin) : forget(@admin)
            flash[:notice]="Login Successfull"
            flash[:color]="valid"
            redirect_to :action => "new", :controller => "admins"
        else
            flash[:notice]="Login Failed"
            flash[:color]="invalid"
            render 'index'
        end
    end
    def removeuser
        session[:user_id] = nil
        cookies.delete :user_id
        flash[:notice]="user logged out successfully"
        flash[:color]="valid"
        redirect_to :action => 'index', :controller => 'homes'
    end
end

session_helper.rb

module SessionsHelper
    def remember(admin)
    admin.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end
  def forget(admin)
    admin.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end
end

模型/ admin.rb

class Admin < ActiveRecord::Base
  attr_accessible :email, :password_hash, :password_salt, :picture, :user_name,:password_confirmation,:password, :remember_me
  attr_accessor :password
  before_save :encrypt_password
  mount_uploader :picture, PictureUploader
  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 :user_name, :presence => true, :length => {:in => 3..10}
  validates :password, :confirmation => true
 validates_length_of :password, :in => 6..20, :on => :create
 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
  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
end

由于我是RoR的新手,请帮助我解决此错误并成功运行此应用。

1 个答案:

答案 0 :(得分:0)

您必须在SessionsController中包含SessionsHelper,或将remember和forget方法移动到SessionsController。