hey
hey

Reputation: 55

Rails search form in different controller?

Okay say I have a simple search form for Projects records like in the railscast found here: http://railscasts.com/episodes/37-simple-search-form?autoplay=true

Can you put the form_tag for the search in a different view other than the projects index.html? Like in a static_pages controller type of deal view? And how could you redirect to the projects index.html view after submitting the search in such a case? Thanks

Upvotes: 1

Views: 410

Answers (1)

Nickey
Nickey

Reputation: 199

Add below code wherever you want to add search functionality.

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

Changes needed in your controller only. Try render view instead of redirect and you are done.

def index
  @projects = Project.search(params[:search])
  render `projects/index`
end

Upvotes: 1

Related Questions