Ruby on Rails:ApplicationController方法未定义? (初学者)

时间:2015-12-03 18:17:55

标签: ruby-on-rails ruby controller

logged_in?方法在PrescriptionsController中显示为undefined,但在UsersController中不显示。这真的很奇怪,因为它们都扩展了ApplicationController,其中存在logged_in_user方法。 logged_in?方法在SessionsHelper模块中。

this.setState({dob: Object.assign({}, this.state.dob, { year: '1980'} })

错误:

class PrescriptionsController < ApplicationController
  before_action :logged_in_user, only: [:index] //this shows up as undefined

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:edit, :update, :index]  //but this is ok

class ApplicationController < ActionController::Base
   # Confirms a logged-in user.
   def logged_in_user
       unless logged_in?
           flash[:danger] = "Please log in."
           redirect_to login_url
       end
   end

module SessionsHelper
   # Returns true if the user is logged in, false otherwise.
 def logged_in?
   !current_user.nil?
 end

1 个答案:

答案 0 :(得分:1)

在你的application.rb里面放了这个

class ApplicationController
  include SessionsHelper
end

另请参阅此SessionsHelper in railstutorial.org: Should helpers be general-purpose modules for code not needed in views?