0

I am a begiiner in RoR. I am trying to create links on a page. I want multiple links on the page and pass a parameter to link's url based on which link was clicked.

I tried doind this:

routes.rb:

get '/search/facet/:param' => 'product_search#facet'

view:

<%= link_to "MORE", /search/facet/(param => taxon)

product_search_controler.rb:

def facet
    @facetVariable = "hello"
    @taxonVariable = params[:param]
end

But whenever I click on the MORE link, I get a 404. Also, the linked page has url /search/facet and not /search/facet/param=taxon which I want.

Please could someone point out my mistake

nish
  • 6,952
  • 18
  • 74
  • 128

2 Answers2

2

You can use named routes:

get '/search/facet/:param' => 'product_search#facet', :as => :product_search_facet

and in your view, assuming taxon is a local variable:

<%= link_to 'MORE', product_search_facet_path(taxon) %>

Rails guides, for reference:

http://guides.rubyonrails.org/routing.html

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
1

You might want to try this:

get '/search/facet/:param' => 'product_search#facet', :as=>:search

<%= link_to "MORE", search_url(:param => taxon)
Satya Kalluri
  • 5,148
  • 4
  • 28
  • 37
  • Also,if you want taxon as a string, you need to use <%= link_to "MORE", search_url(:param => 'taxon') – Satya Kalluri Dec 02 '13 at 10:52
  • This gives '/search/facet/taxon` and not `search/facet/param=taxon`. Will I still be able to use params[:param] to get the value of taxon in the controller? – nish Dec 02 '13 at 11:12
  • Yes you will. The PARAM in the Route pattern is a place holder. You can use params[:param] to get the value 'taxon'. Give it a shot buffy. – Satya Kalluri Dec 02 '13 at 11:14
  • it shows nil on the view, my view is '/product_search/facet.html.erb'. `

    <%=@facetVariable.inspect%>

    <%=@taxonVariable.inspect%>`. It is showing nil for both variables

    – nish Dec 02 '13 at 11:16