我想创建一个Current_User方法,但我不想使用gem或类似方法。我将如何在Rails 4.1.2
中做到这一点Questions_Controller我想要current_user方法。
class QuestionsController < ApplicationController
before_filter :auth, only: [:create, :your_questions, :edit, :update]
# def index
# @question = Question.new
# @questions = Question.unsolved(params)
# end
@questions = current_user.your_questions(params[:page])
def your_questions(page)
questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
end
def self.unsolved(params)
order('created_at DESC').where(solved: false).paginate(page: params[:page],per_page: 3)
end
def create
@question = current_user.questions.build(params[:question])
if @question.save
flash[:success] = 'Your question has been posted!'
redirect_to @question
else
@questions = Question.unsolved(params)
render 'index'
end
end
def new
@question = Question.new
end
def show
# raise FOO
puts params
@question = Question.find(params[:id])
@answer = Answer.new
end
def your_questions
@questions = current_user.your_questions(params[:page])
# current_user.your_questions(params[:id])
end
def edit
@question = current_user.questions.find(params[:id])
end
def update
@question = current_user.questions.find(params[:id])
if @question.update_attributes(params[:question])
flash[:success] = 'Your question has been updated!'
redirect_to @question
else
render 'edit'
end
end
def search
@questions = Question.search(params)
end
end
我的用户模型
class User < ActiveRecord::Base
has_many :questions
has_many :answers
# attr_accessible :username, :password, :password_confirmation
has_secure_password
# validates :username, presence: true, uniqueness: { case_sensitive: false },
# length: { in: 4..12 },
# format: { with: /A[a-z][a-z0-9]*z/, message: 'can only contain lowercase letters and numbers' }
validates :password, length: { in: 4..8 }
validates :password_confirmation, length: { in: 4..8 }
def your_questions(page)
questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
end
end
我的应用程序控制器
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
# protect_from_forgery
helper_method [:current_user, :logged_in?]
protected
private
def login(user)
session[:user_id] = user.id
end
def current_user
current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
def auth
redirect_to login_url, alert: 'You must login to access that page' unless logged_in?
end
end
如果有更多文件需要我添加到问题中,请在rails开发者评论我是一个新手ruby:)
答案 0 :(得分:5)
class ApplicationController < ActionController::Base
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
答案 1 :(得分:2)
我认为您的问题不在控制器中,而在于您的助手。
正在调用寻找局部变量current_user
的方法而且没有一个方法。你需要像这样实例化这些:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method [:current_user, :logged_in?]
private
def login(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!current_user.nil?
end
def auth
redirect_to login_url, alert: 'You must login to access that page' unless logged_in?
end
end
喜欢@RafalG。声明请参阅@
前面的current_user
。这将创建一个实例变量来跟踪,而不是引用缺少的局部变量。
另请注意,您当前的current_user
方法将始终运行User.find
,因为本范围内的局部变量current_user
始终为nil
,因此您需要进行此操作实例的一部分。
<强>更新强>
我将保留以上内容以进行启发,因为您仍应创建实例。我认为这是真正的问题
class QuestionsController < ApplicationController
before_filter :auth, only: [:create, :your_questions, :edit, :update]
# def index
# @question = Question.new
# @questions = Question.unsolved(params)
# end
#vvvv This Line is out of a scope and will raise errors vvv#
@questions = current_user.your_questions(params[:page])
def your_questions(page)
questions.paginate(page: page, order: 'created_at DESC', per_page: 3)
end
....
end
如果你想这样做,你会在before_filter
回调中声明它,因为现在Rails
不知道如何恰当地处理这个语句,并且在一个它无法访问的方法之外你的任何助手。