简体   繁体   English

在一个方法中传递时,使变量在控制器的所有方法中可用

[英]make variable available in all method of controller when passed in one method

Here i send the user.id as params dd 在这里我发送user.id为params dd

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

In controller empassets i fetch it by 在控制器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

I need this @id in all new show edit method. 我在所有新的节目编辑方法中都需要这个@id。 But it takes in index only as i mention it in index. 但它仅在我在索引中提及它时才需要索引。 How can i make such that if Lend asset is click then @id= params[:id] have value in all methods. 如何点击Lend资产,那么@ id = params [:id]在所有方法中都有价值。 How can it is possible to make it available for another @id = params[:id] is not send in that controller? 怎么可能让它可用于另一个@id = params [:id]不在该控制器中发送?

Maybe will be better if you store the current user in session and after that, capture the user model with a filter in the controller, like this: 如果将当前用户存储在会话中并在此之后,使用控制器中的过滤器捕获用户模型可能会更好,如下所示:

# 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

Basically, my idea is store that information inside a model with a filter, you can use session if you wanth to get the information (user id). 基本上,我的想法是将信息存储在带有过滤器的模型中,如果您想获取信息(用户ID),可以使用会话。

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

Note I used session[:user_id] || params[:dd] 注意我使用了session[:user_id] || params[:dd] session[:user_id] || params[:dd] because maybe, the session information was not stablished and you give it :dd parameter. session[:user_id] || params[:dd]因为可能,会话信息没有建立,你给它:dd参数。 But if you want to stablish the @id variable, you can use a filter like before. 但是如果你想要建立@id变量,你可以像以前一样使用过滤器。

But I don't know what is the main problem. 但我不知道主要问题是什么。

Edit 编辑

# 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

Sorry for the delay. 抱歉耽搁了。

Unless you want to store it to the session, there is no way to automatically make @id available to every method in the controller.. but you can add the param to each link/form, like so: 除非您想将它存储到会话中,否则无法自动使@id可用于控制器中的每个方法..但您可以将param添加到每个链接/表单,如下所示:

<%= link_to("New Asset", {:controller => 'empassets', :action=> 'new', :dd => @id})%>

<%= link_to("Show Asset", {:controller => 'empassets', :action=> 'show', :dd => @id})%>

where these links are in the index view and @id is set in the index method. 这些链接在索引视图中,@ @id在索引方法中设置。

Not exactly sure what the goal is here, but something like this may work. 不完全确定目标是什么,但这样的事情可能有效。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Rails 3.2的视图和控制器中使用下划线方法? - How to make underscore method available in view and controller in rails 3.2? 在helper_method之后的控制器中的方法是否可用于rails中的所有视图 - Are methods in controller after helper_method available to all views in rails 为什么可以在一个控制器中使用助手的实例方法,而在另一个控制器中却不能使用它? - Why would an instance method from a helper be available in one controller but not another? 如何使一种方法可用于所有控制器? 以及所有型号如何? - How to make a method available to all controllers? And how to all models? NameError - 在 controller 中调用创建的方法时未定义的局部变量或方法 - NameError - undefined local variable or method when calling created method in controller Ruby gem方法只在控制器中可用? 如果是这样,如何在其他文件中使用它? - Ruby gem method only available in controller? If so, how to make it available in other files? 使方法适用于所有模型 - Making a method available to all models 在Controller中将变量的值从一个方法传递到另一个方法 - Passing value of variable from one method to another in a Controller 控制器方法显示一个字段的所有实例,没有重复的红宝石 - Controller method to show all instances of one field no duplicates ruby Rails 4 Controller for Reviews Create方法传递了所有正确的参数,但是创建不正确 - Rails 4 Controller for Reviews Create method is passed all the correct parameters however it does not create properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM