[英]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.