dan
dan

Reputation: 1080

Rails - passing arbitrary parameters into search

I have passed a value into a search page via -

<%= link_to 'add', users_path(bookto: @book.id) %>

in the view and

@book = Book.find_by_id(params[:bookto])

in the receiving controller action.

I have a search form in my receiving (index) view

<%= form_tag users_path(params[bookto: @book.id]), method: 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", name: nil %>
  </p>
<% end %>

<%= "#{@book.id}, #{@book.title}" %>

<% if @users %>
    <%= render partial: 'layouts/showusers', locals: {users: @users} %>
<% end %>

When I navigate through to the page

http://localhost:3000/users?bookto=1

The value of @book is passed properly. However, when I perform a search, the parameter is not being passed to

http://localhost:3000/users?utf8=✓&search=mysearch

I'm not passing the parameter through. I don't want to use this arbitrary parameter in the search, I just want it available to me to use once the search is complete. How do I achieve this? Do I need to add a search action to my controller?

Upvotes: 1

Views: 167

Answers (1)

The Lazy Log
The Lazy Log

Reputation: 3574

Why don't just add a hidden field inside your search form like this

<%= form_tag users_path, method: 'get' do %>
  <p>
    <%= hidden_field_tag :bookto, @book.id %>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", name: nil %>
  </p>
<% end %>

Because you would want to see bookid in your URL anyway, so this method is ok in your case.

Upvotes: 1

Related Questions