如何在index.html.erb上调用带超链接的方法?
我目前有一个搜索字段,允许用户输入关键字。这将搜索模型中的属性并显示找到的任何属性。
例如,我有record_controller.rb,其中包含:
def index
@records = Record.search(params[:search])
end
app / models / record.rb有:
# Added for the search
def self.search(search)
where("title LIKE ? OR keyword LIKE ? OR description LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
end
' title,' keyword'和' decscription'是模型中的属性。
app / views / records / index.html.erb包含搜索表单:
<%= form_tag(records_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: " Search" %>
<%= submit_tag "Find" %>
<% end %>
在同一个index.html.erb页面上,如何添加一个超链接,显示包含我指定标题的模型中的所有数据?
例如显示所有具有标题&#34; truck&#34;的行。 如果用户单击超链接,它将显示数据
where('title LIKE "truck"', "%#{search}%")
我假设我还需要在记录模型中添加一个新方法,例如:
# I'll have to play with this because I know it is wrong
# Added for the search
def self.searchtruck(search)
where('title LIKE "truck"', "%#{search}%")
end
答案 0 :(得分:1)
这可以满足您的需求,并使用group
param[:search] = 'truck'