Phil Walker
Phil Walker

Reputation: 11

Redirect after search form submit Rails 4

I've built a custom search in a navbar that submits to my index action that works great when I'm at my index view. But once i'm in a different view, the params are correctly submitted, but I need to redirect to my index view. Thus far i've been unable to figure it out. Any help would be VERY appreciated!

My search function in my model

def self.search(search_option, search_text)
  where("#{search_option} like ?", "#{search_text}%")
end

controller:

def index
  if params[:search_text]
    @books = Book.search(params[:search_option].downcase, params[:search_text].capitalize)
  else
    @my_books = current_user.books
  end
end

search form in view:

<%= form_tag books_path, method: :get do %>
    <div class="form-group">
      <% options_array = ["Genre","Category"] %>
      <%= select_tag :search_option, options_for_select(options_array), class: "btn btn-default dropdown-toggle select-options" %>
      <%= text_field_tag :search_text, params[:search], class: "form-control" %>
    </div>
    <%= submit_tag "Search", :name=>nil, class: "btn btn-default" %>
  <% end %>

Upvotes: 0

Views: 396

Answers (1)

Phil Walker
Phil Walker

Reputation: 11

Real new developer mistake here, but the problem was I was putting my search form into a Bootstrap navbar, inside existing <form></form> tags. This is short circuiting my search. Deleted those and it worked just as expected!

Upvotes: 1

Related Questions