我跟随Ryan Bates' guide on search functionality。我现在忽略了搜索算法的实现,只是返回到目前为止它正在做它应该做的事情,唯一的问题是现在当我访问/ posts时,我会自动重定向到/ posts/1。Rails:重定向仅在表单提交后显示
在我的职位控制器:
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
在我index.html.erb:
<%= form_tag posts_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
在我Posts.rb
def self.search(search)
#insert search method here
return Post.find_by_id(1)
end
我怎样才能得到这个让我可以访问/发布并在我的表单中搜索而不会自动重定向?
很酷。不是最干净的解决方案,但它绝对有效。 – varatis 2012-02-01 05:47:35