I’m following Ryan Bates’ guide on search functionality. I’ve left out the implementation of the search algorithm right now, only returning So far it’s doing what it should, the only problem is that now when I visit /posts, I get automatically redirected to /posts/1.
In my Posts controller:
def show
end
def index
@post = Post.search params[:search]
puts ("----------------" + @post.to_s + "-----------")
respond_to do |format|
format.html { redirect_to @post }
end
end
In my index.html.erb:
<%= form_tag posts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
In my Posts.rb
def self.search(search)
#insert search method here
return Post.find_by_id(1)
end
How can I get this so that I can visit /posts and search in my form without being automatically redirected?
The only way from your implementation would be to check for params[:search]. This would be nil if you just went to /posts