views:

224

answers:

2

Hi, I'm creating in my index page of my ruby on rails program, a list of the most commonly searched for terms in my database and hence each time a user selects a specific category this is written to another database.

What i would like it to create a hyperlink and pass a certain amount of parameters to a form like is usually done with a select_tag but instead with just a hyperlink, i would like to pass a set of hidden fields that i have on the page as well as what the user has selected.

To give you a better idea, basically i have the following structure in my program:

User inputs a search on (index.html.erb), user clicks on submit tag action, user is taken to search.html.erb page and is displayed a set of refined categories + some fields, submit button,

user is taken to closest.html.erb (which uses parameters from the previous form by invoking the params[:searchSelected] and a few other params. )

I would also like to add this functionality: Mimick this same operation, but instead of going in the search.html.erb, i would click on an already refined search category on the index.html.erb page (from a link_to , transmit as parameters which link_to the user has chosen + the hidden fields.

i Currently have this code

@stats.each do
|scr|%>
<%= link_to scr.category,  :action => 'closest', :id => scr.category%>

I'm not sure if this is relevant, but i currently have the following routes in my routes.rb file

 map.resources :stores, :collection => { :search => :get }

 map.connect ':controller/:action/:id'
 map.connect ':controller/:action/:id.:format'

would anyone please assist me please? this is my first ruby on rails project and i would really like to find a way around this please

A: 

i managed to resolve this by taking the params[:id] and then according to the value either set my own values (instead of the hidden ones in the index.erb which i had set manually anyway) and otherwise, continue as usual had i placed a regular search

Erika
+1  A: 

I am not sure if this is what you were thinking, but you can add additional parameters to the link_to tag. They are then available in your controller. So:

<%= link_to scr.category,  :action => 'closest', :id => scr.category, :other_param => "test" %>

Will be available in your controller.

def closest
  params[:other_param] == "test" #this will be true
end
MattMcKnight
this is a great tip! will definitely use in the future, thanks!
Erika