trowse
trowse

Reputation: 204

Route to Current Rails page for search

I have a sidebar that fetches articles in my application_controller but I want to add a search function to it so I modified it to look like this

  def fetch_articless
    @articles = Article.search(params[:search])
  end

The search works fine, but I currently have my form submitting to my application root so if you search, it always redirect to the root.

<%= form_tag root_path, :method => 'get' do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :title => nil %>
  </p>
<% end %>

Since my sidebar is on every page I would like to be able to submit my form to whatever page I am currently on and the search would be performed through the application helper. I assume I need to make a new route but being new to Rails I don't really understand how routes work yet.

Any help is greatly appreciated.

Upvotes: 0

Views: 53

Answers (1)

Mark Swardstrom
Mark Swardstrom

Reputation: 18080

the method url_for will return the current url. And, you can always use '' (empty string) for the form action to submit to the current page

Note - with search boxes, I tend to use the <form> tag (no erb) so it doesn't include the special character (it's a snowman*), and I use a regular input submit tag so it doesn't submit the name=value of the button.

Upvotes: 1

Related Questions