notaceo
notaceo

Reputation: 1093

Rails link_to interpreting :class and :style as params for the :action?

Here is the offending line:

<p><%= link_to 'Job Type template', :action => :jt_template, :style => "color:white", :class => "btn btn-primary" %></p>

And when we load the page and inspect the element:

<a href="/generals/jt_template?class=btn+btn-primary&amp;style=color%3Awhite">Job Type template</a>

Why are my class and style tags being interpreted as params for the action?

Other than this weirdness - it's working as expected.

Just FWIW - the jt_template action sends the CSV file as a download. I was linking directly to the file stored in my public dir, but I found that when you clicked the button in Safari it would just open the CSV in the browser, and I'm forcefully required to make the file download instead (even though a user could get from the opened page)

EDIT:

Ever do that thing where you post to SO, then think of some new wording you haven't Googled, then find your answer on the first result?

Add css class to rails link_to helper

Someone go ahead and post the answer "you should put your :action inside { } and it will work" and I'll accept that.

Upvotes: 0

Views: 66

Answers (1)

notaceo
notaceo

Reputation: 1093

Do this:

<p><%= link_to 'Job Type template', {:action => :jt_template}, :style => "color:white", :class => "btn btn-primary" %></p>

:action and :controller must be placed between { } in order to avoid the incorrect interpolation of stuff that comes after it (I only tried with :class and :style, but I assume it would affect anything that follows)

Upvotes: 1

Related Questions