ruby-on-rails - 在一个方法中传递时,使变量在 Controller 的所有方法中可用

标签 ruby-on-rails ruby ruby-on-rails-3

这里我将 user.id 作为参数 dd 发送

<h3><%= link_to("Lend Asset", {:controller => 'empassets', :action=> 'index', :dd => user.id})%></h3>

在 Controller empassets 中,我通过

获取它
  def index
     @id = params[:dd]
     @empassets = Empasset.where(:ad => @id)
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @empassets }
    end
  end

  def show
     @id = params[:dd]
    @empasset = Empasset.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @empasset }
    end
  end

  def new
     @id = params[:dd]
    @empasset = Empasset.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @empasset }
    end
  end

  def edit
     @id = params[:dd]
    @empasset = Empasset.find(params[:id])
  end

我在所有新的节目编辑方法中都需要这个@id。但它只接受索引,因为我在 index.html 中提到了它。我怎样才能做到,如果单击“借出 Assets ”,那么@id= params[:id] 在所有方法中都具有值(value)。怎么可能使另一个 @id = params[:id] 不在该 Controller 中发送?

最佳答案

如果将当前用户存储在 session 中,然后在 Controller 中使用过滤器捕获用户模型,可能会更好,如下所示:

# controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :set_current_user_in_model

  private
  def current_user
    @current_user ||= User.find(params[:dd]) || User.new
  end

  # This method save the current user in the user model, this is useful to have access to the current user from a model and not from the controller only
  def set_current_user_in_model
    User.current_user current_user if not current_user.nil?
  end
end

# models/user.rb
class User < ActiveRecord::Base
  #...
  # This is useful to get the current user inside a model
  def self.current_user(user = nil)
    @@current_user = (user || @@current_user)
  end
  #...
end

基本上,我的想法是将该信息存储在带有过滤器的模型中,如果您想获取信息(用户 ID),可以使用 session 。

def index
  session[:user_id] = params[:dd]
  @empassets = Empasset.where(:ad => session[:user_id])
  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @empassets }
  end
end

def show
  @empasset = Empasset.find(session[:user_id] || params[:dd])

  respond_to do |format|
    format.html # show.html.erb
    format.json { render json: @empasset }
  end
end

注意我使用了session[:user_id] || params[:dd] 因为可能 session 信息没有建立,你给它 :dd 参数。但是如果你想建立 @id 变量,你可以像以前一样使用过滤器。

但我不知道主要问题是什么。

编辑

# controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :set_dd_param, :except => :index
  def index
    session[:dd] = params[:dd] # Here you write the session
    @current_user ||= User.find(params[:dd]) || User.new
  end
  # ...
  protected
  def set_dd_param
    params[:dd] = session[:dd] || -1 # Here you read the session a write the params variable
  end
end

抱歉耽搁了。

关于ruby-on-rails - 在一个方法中传递时,使变量在 Controller 的所有方法中可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13230787/

相关文章:

javascript - Rails AJAX 远程 : true with dynamic id

ruby-on-rails - environment.rb 中缺少 Rails 路径名

ruby-on-rails - Rails 3 时区混淆

ruby-on-rails - 无法加载 lib 目录模块 - 未初始化的常量 - rails 2 到 rails 3 升级

ruby - 有没有一种简单的方法可以在 Ruby 中复制多维数组?

ruby-on-rails - 如何创建和管理用户?

ruby-on-rails - 事件管理员 - 使资源可排序以在前端进行特定排序

ruby-on-rails - 向 Rails 模型实例对象动态添加哈希属性?

适用于 Ruby on Rails 3 的 Android 谷歌云消息传递 gem

ruby-on-rails - SSL_connect returned=1 errno=0 state=SSLv3 读取服务器证书 B : certificate verify failed