2

I want to pass a parameter (for example the param is called company=heise) on a link to, but only if the param really is present.

Lets say:

I visit somesite.com and click a link that would redirect me to mysite.com/?company=heise On mysite i have a few link_to's and I want them to pass the parameter company=heise when it's present and since it is present now because I entered the site via mysite.com/?company=heise it should do the following:

<%= link_to "This is a link", this_link_path, class: tl(this_link_path) %>

should redirect me to mysite.com/this_link/?company=heise

  • If company=heise is set I want to display them in the URL and I furthermore want to not display it when it's not set.

Hope I made my question clear enough

benl96
  • 274
  • 3
  • 18

2 Answers2

3

Conditionally pass a hash, containing additional params for this_link_path url helper.

<%= link_to "This is a link", this_link_path( ({company: params[:company] } if params[:company]) ), class: tl(this_link_path) %>

To be more concise you can compact the hash.

<%= link_to "This is a link", this_link_path({company: params[:company]}.compact), class: tl(this_link_path) %>

If you know that you'll need it more often, wrap this_link_path call in a custom helper. Hash can contain additional params, including ones with fixed values.

def this_link_with_additional_params_path
  this_link_path({company: params[:company], name: 'test'}.compact) 
end

Then in view you can use:

<%= link_to "This is a link", this_link_with_additional_params_path, class: tl(this_link_path) %>
Bartosz Pietraszko
  • 1,367
  • 9
  • 9
  • Yes, i do know that I need to use this more often. Is there any way to provide another param that is always set? For example: I always want have a param called name=test set, and the company one only if the condition is true. – benl96 Aug 31 '18 at 12:36
  • @benl96 Updated answer. – Bartosz Pietraszko Aug 31 '18 at 12:50
  • Thank you really much! That worked just fine. Only problem left now is while using anchors. mysite.com/test/?company=heise is my entry URL - now I want to go to a specific anchor but the URL looks like this after clicking on the anchor link mysite.com/test.company=heise#anchor instead of mysite.com/test/?company=heise#anchor. Do you know how to get the correct URL for anchors as well? – benl96 Aug 31 '18 at 13:03
  • https://stackoverflow.com/questions/4108082/get-anchor-part-of-url-in-controller-in-ruby-on-rails unfortunately, you can't read them on Rails side. – Bartosz Pietraszko Aug 31 '18 at 13:22
0

The idea here is to create a helper method to manage the params that you want to send to this link conditionally.

# some_helper.rb
def carried_over_params(params = {})
  interesting_keys = %i(company)
  params.slice(*interesting_keys).compact
end

Afterwards, you should use this in the view

<%= link_to "This is a link", this_link_path(carried_over_params(params).merge(test: 'value')), class: tl(this_link_path) %>
khaled_gomaa
  • 3,382
  • 21
  • 24
  • Seems like this works! Could I provide the link_to a extra param that is always set? For example I want another param on this link to with the name name and the value test --- This resolves in syntax error obviously --- <%= link_to "This is a link", this_link_path(name: 'test' carried_over_params(params)), class: tl(this_link_path) %> – benl96 Aug 31 '18 at 12:43
  • updated the answer to address what you are asking here. Please add it to your original question. – khaled_gomaa Sep 01 '18 at 13:38