regmiprem
regmiprem

Reputation: 735

make variable available in all method of controller when passed in one method

Here i send the user.id as params dd

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

In controller empassets i fetch it by

  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. 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. How can it is possible to make it available for another @id = params[:id] is not send in that controller?

Upvotes: 1

Views: 1180

Answers (3)

Andr&#233;s
Andr&#233;s

Reputation: 624

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).

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] because maybe, the session information was not stablished and you give it :dd parameter. But if you want to stablish the @id variable, you can use a filter like before.

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.

Upvotes: 4

Hachi
Hachi

Reputation: 173

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

Upvotes: 0

aguynamedloren
aguynamedloren

Reputation: 2273

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:

<%= 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.

Not exactly sure what the goal is here, but something like this may work.

Upvotes: 0

Related Questions