我希望我的搜索表单适用于所有 URL,而不是重定向到消息页面。例如在搜索“hi”时:
on /messages return /messages?utf8=✓&search=hi&commit=Search
on /posts return /posts?utf8=✓&search=hi&commit=Search
在我的应用程序视图中(引导样式):
<%= form_tag(messages_path, :method => "get", class: "navbar-form navbar-left") do %>
<div class="form-group">
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
</div>
<%= submit_tag "Search", class: "btn btn-default" %>
<% end %>
消息控制器:
def index
if params[:search]
@messages = Message.search(params[:search]).order("created_at DESC")
else
@messages = Message.all.order("created_at DESC")
end
end
用户控制器:
def index
if params[:search]
@users = User.search(params[:search]).order("created_at DESC")
else
@users = User.all.order("created_at DESC")
end
end
消息模型:
def self.search(search)
where("title LIKE ?", "%#{search}%")
end
用户型号:
def self.search(search)
where("username LIKE ?", "%#{search}%")
end
如果我将参数复制并粘贴(?utf8=✓&search=hi&commit=Search)
到页面上,它们就可以正常工作。我尝试messages_path
从表单中删除,但出现错误。如何使用根据我使用的控制器更改的表单路径?