user502052
user502052

Reputation: 15259

Is it possible to pass variables to actions using Ruby on Rails 3?

I am using Ruby on Rails 3.

In my controller I have (can I have?!):

def action_name(variable_name)
  ...
end

How can I pass the value of the "variable_name" using this syntax

[...] :url => { :action => :action_name("name") } [...]

? Is it possible?

Upvotes: 4

Views: 2432

Answers (3)

Siwei
Siwei

Reputation: 21557

I suggest you use session or flash. e.g.

def action1
  session[:your_key] = some_object
  redirect_to some_action_path
end

def action2
  @some_object = session[:your_key]
  # ...
end

Upvotes: 1

khelll
khelll

Reputation: 24010

I'm not sure this is possible. However, why don't you use the params hash instead to get that param?

Upvotes: 1

Nikita Barsukov
Nikita Barsukov

Reputation: 2984

In view: :url => {:action => :action_name, :variable_name => 'dog'}

In controller:

def action_name
  variable_name = params[:variable_name]
  @something = Something.where(:name => variable_name)
  #More code in this method
end

This code will pass variable_name as a part of querystring in URL: http://site.com/something?variable_name=dog Obviously, don't use this approach for sensitive data, and use :session[:variable_name] instead.

Upvotes: 13

Related Questions