在 rails 中使用调查员 gem 实施调查

Implementation of survey using surveyor gem in rails

我正尝试在 rails 中使用调查员 gem 进行调查。我想利用用户 ID 来跟踪哪个用户创建了调查以及哪个用户对哪个调查给出了什么样的响应。

问题是我没有使用 Devise gem 进行用户登录和注册。我手动构建它。测量员 gem 使用 Devise 的辅助方法 current_user,其中 returns 详细介绍了当前用户。

因为我没有使用 devise,所以我不确定在哪里添加辅助方法 current_user。

我不太确定 post 的代码是什么,所以请评论所需的详细信息。我会根据需要编辑我的 post。

谢谢!

application_controller.rb

  class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :authorize
helper_method :current_user
protected
  def authorize
    return true if ((self.class == SessionsController)|| (self.class == UsersController && (self.action_name == "new" || self.action_name == "create")))
    unless (User.find_by_id(session[:user_id]))
        redirect_to url_for(:controller => :sessions , :action => :new), alert: "You need to be logged in."
    end
   end

def current_user
    @current_user = User.find(session[:user_id])
end
end

这里是使用current_user方法的测量员gem控制器的link:https://github.com/kjayma/surveyor_gui/blob/master/app/controllers/surveyor_gui/survey_controller.rb

这是实现 current_user 方法的一种可能解决方案。

helper_method 将使 current_user 方法在每个控制器中可用,它继承自 ApplicationController.

class ApplicationController

  helper_method :current_user

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
end