Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 365
user502052 Avatar asked Dec 07 '22 23:12

user502052


1 Answers

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.

like image 121
Nikita Barsukov Avatar answered Dec 09 '22 11:12

Nikita Barsukov