Ruby on rails 3 如何在不使用任何gem或插件的情况下创建当前用户方法?

Ruby on rails 3 如何在不使用任何gem或插件的情况下创建当前用户方法?,ruby-on-rails-3,methods,Ruby On Rails 3,Methods,我知道这很愚蠢,但我想知道我们如何创建一个当前用户方法,在不使用任何gem或插件的情况下访问整个应用程序?为了测试它,我创建了一个应用程序,使用户能够共享文件和文件夹。如何创建这样的方法,用户只能访问他的文件夹和文件?以下是我的代码示例: 登录控制器: class LoginController < ApplicationController layout 'signup' #to skip checking the authentication and authorization.

我知道这很愚蠢,但我想知道我们如何创建一个当前用户方法,在不使用任何gem或插件的情况下访问整个应用程序?为了测试它,我创建了一个应用程序,使用户能够共享文件和文件夹。如何创建这样的方法,用户只能访问他的文件夹和文件?以下是我的代码示例:

登录控制器:

class LoginController < ApplicationController
 layout 'signup'
  #to skip checking the authentication and authorization.
  skip_before_filter  :check_authentication, :check_authorization

  def index
  end

  def authenticate
        if request.post?
            user = User.authenticate(params[:username],params[:password])
            if user
        session[:current_user_id]=user.id
        session[:name]= user.first_name
                puts "session name #{session[:name]}"
                redirect_to(:subdomain => user.company.subdomain, :controller => :dashboard)
      else
        flash.now[:notice] = "Invalid user/password combination"
      end  

        end
  end

  def destroy
    session[:current_user_id] = nil
    reset_session
    flash[:notice] = "You have been successfully logged out."
    redirect_to root_url
  end

end 
class ApplicationController < ActionController::Base
      include UrlHelper 
      #include ApplicationHelper
      helper_method :current_user #make this method available in views
      protect_from_forgery


#  def current_user
#    @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
#  end



end
class TasksController < ApplicationController


  # GET /tasks
  # GET /tasks.xml
  def index
    @menu = "Timesheet"
    @page_name = "Manage Task"
    company_id = Company.find_by_subdomain(request.subdomain)
    @users = User.find_all_by_company_id(company_id)
    @tasks = current_user.tasks.all#Task.all
    @task = Task.new

    respond_to do |format|
      format.html # index.html.erb
      format.html # new.html.erb
      format.xml  { render :xml => @tasks }
    end
  end
end
应用程序控制器:

class LoginController < ApplicationController
 layout 'signup'
  #to skip checking the authentication and authorization.
  skip_before_filter  :check_authentication, :check_authorization

  def index
  end

  def authenticate
        if request.post?
            user = User.authenticate(params[:username],params[:password])
            if user
        session[:current_user_id]=user.id
        session[:name]= user.first_name
                puts "session name #{session[:name]}"
                redirect_to(:subdomain => user.company.subdomain, :controller => :dashboard)
      else
        flash.now[:notice] = "Invalid user/password combination"
      end  

        end
  end

  def destroy
    session[:current_user_id] = nil
    reset_session
    flash[:notice] = "You have been successfully logged out."
    redirect_to root_url
  end

end 
class ApplicationController < ActionController::Base
      include UrlHelper 
      #include ApplicationHelper
      helper_method :current_user #make this method available in views
      protect_from_forgery


#  def current_user
#    @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
#  end



end
class TasksController < ApplicationController


  # GET /tasks
  # GET /tasks.xml
  def index
    @menu = "Timesheet"
    @page_name = "Manage Task"
    company_id = Company.find_by_subdomain(request.subdomain)
    @users = User.find_all_by_company_id(company_id)
    @tasks = current_user.tasks.all#Task.all
    @task = Task.new

    respond_to do |format|
      format.html # index.html.erb
      format.html # new.html.erb
      format.xml  { render :xml => @tasks }
    end
  end
end
class ApplicationController
在任务控制器中:

class LoginController < ApplicationController
 layout 'signup'
  #to skip checking the authentication and authorization.
  skip_before_filter  :check_authentication, :check_authorization

  def index
  end

  def authenticate
        if request.post?
            user = User.authenticate(params[:username],params[:password])
            if user
        session[:current_user_id]=user.id
        session[:name]= user.first_name
                puts "session name #{session[:name]}"
                redirect_to(:subdomain => user.company.subdomain, :controller => :dashboard)
      else
        flash.now[:notice] = "Invalid user/password combination"
      end  

        end
  end

  def destroy
    session[:current_user_id] = nil
    reset_session
    flash[:notice] = "You have been successfully logged out."
    redirect_to root_url
  end

end 
class ApplicationController < ActionController::Base
      include UrlHelper 
      #include ApplicationHelper
      helper_method :current_user #make this method available in views
      protect_from_forgery


#  def current_user
#    @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
#  end



end
class TasksController < ApplicationController


  # GET /tasks
  # GET /tasks.xml
  def index
    @menu = "Timesheet"
    @page_name = "Manage Task"
    company_id = Company.find_by_subdomain(request.subdomain)
    @users = User.find_all_by_company_id(company_id)
    @tasks = current_user.tasks.all#Task.all
    @task = Task.new

    respond_to do |format|
      format.html # index.html.erb
      format.html # new.html.erb
      format.xml  { render :xml => @tasks }
    end
  end
end
class TasksController@tasks}
结束
结束
结束
我收到的错误信息是:

NameError in TasksController#index

undefined local variable or method `current_user' for #<TasksController:0xfa7e638>
TasksController#索引中的
name错误
未定义的局部变量或方法“当前用户”#
这并不难;)只需定义所需的方法:

class ApplicationController < ...
  def current_user
    @current_user ||= session[:current_user_id] && User.find_by_id(session[:current_user_id]) # Use find_by_id to get nil instead of an error if user doesn't exist
  end
  helper_method :current_user #make this method available in views
end
类应用程序控制器<。。。
def当前用户
@当前用户| |=会话[:当前用户| id]&&user.find_by_id(会话[:当前用户_id])#如果用户不存在,则使用find_by_id获取nil而不是错误
结束
helper_method:当前_用户#使此方法在视图中可用
结束

嗨,朋友们,我找到了不使用任何gem或插件创建
当前用户
方法的方法:

在我的
应用程序\u helper.rb
中,我执行了以下操作:

module ApplicationHelper
  def current_user
    User.find(session[:current_user_id])
  end
end
最后,在我的
应用程序控制器.rb中,我调用了这个,因为我可以从这里通过应用程序访问它:

class ApplicationController < ActionController::Base
  include UrlHelper 
  include ApplicationHelper
  helper_method :current_user
end
class ApplicationController
现在我可以访问与当前用户相关的任何数据:

比如:

@tasks=当前用户。tasks


感谢我所有的朋友提供了宝贵的答案。

Hi Marian正在使用您的代码并在我的任务控制器中以“@tasks=current_user.tasks”的形式应用该方法,然后它会抛出一个错误“undefined method‘tasks’”,我遗漏了什么吗?嗨,Marian,这是我的错误消息:TasksController中的NameError#索引未定义的局部变量或方法“current_user”for#您需要(应用程序)控制器中的方法,如果您将该方法放入帮助器模块中,它将仅在您的视图中可用!嗨,Marian,我按照你说的做了,但这次出现了“nil:NilClass的未定义方法'tasks'错误”