Question

I'm on rails and using the pg_search gem. The search works fine when I'm using an input to pass the param. But when I try to pass the query param from url to my controller to do some advanced filtering, it fails. The url is something like http://localhost:3000/search?q=new+year, and my controller reads params[:q] as new instead of new year. I've been reading the doc and googling for hours but cannot find a solution to my problem. How can I pass the param with a plus sign from url to controller?

Edit: I pass the param as a hidden field using a form. In my view file:

  <% if params[:q] %>
    <input name="q" type="hidden" value=<%= params[:q] %>>
  <% end %>
Was it helpful?

Solution

This has nothing to do with Rails and parameter parsing; you're producing malformed HTML. The problem is that you're missing quotes around your the value of the value attribute of your hidden <input>.

This:

value=<%= params[:q] %>>

needs to be this:

value="<%= params[:q] %>">

Otherwise you're producing something like <input ... value=new year /> where "year" isn't parsed as part of the value attribute.

OTHER TIPS

You shouldn't build a tag like that. Instead use the built-in Rails input tag helpers.

For example:

<% if params[:q] %>
  <%= hidden_field_tag :q, :value => params[:q] %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top