Ruby on Rails passing of parameters between views from link_to tag instead of submit_tags (having both on page)

StackOverflow https://stackoverflow.com/questions/1814481

Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

View:

<%= link_to obj.ptc_devicename ,"/wiuconfig/hd?idval=#{obj.id.to_s}&val=#{@devicetype}",:value => obj.ptc_devicename,:id =>obj.id %><br/>

Controller:

@Heading= params[:val] 
@id=params[:id]

value will be id is 2 and val is @devicetype

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top