【发布时间】:2019-09-26 06:31:05
【问题描述】:
我有一个搜索栏表单,它根据名称搜索项目。项目按已完成的项目、进行中的项目和建议的项目来区分。这三个具有相同的形式期望路径名。所以,我决定在 helper 中使用一个通用的形式。
已完成、提案和进行中项目在项目控制器中使用不同的操作。我试过用params[:name] 区分表格,但它不起作用。有什么解决办法吗?
helper_method
module ProjectsHelper
def form_for_projects_search_bar(condition1,condition2,condition3,&block)
if condition1
form_tag proposed_projects_path, method: :get, :html => {class: "form"}, &block
elsif condition2
form_tag completed_projects_path, method: :get, :html => {class: "form"}, &block
elsif condition3
form_tag projects_path, method: :get, :html => {class: "form"}, &block
end
end
end
表单文件
<%= form_for_projects_search_bar params[:proposed_projects].present?, params[:completed_projects].present?, params[:projects].present? do |form| %>
<%= text_field_tag :term, params[:term],placeholder:"What are you looking for?", class: "search_field" %>
<%= submit_tag 'Search', name: nil, class: "search_bar_button" %>
<% end %>
路线
projects_path GET /projects(.:format) projects#index
proposed_projects_path GET /proposed_projects(.:format) projects#proposal
completed_projects_path GET /completed_projects(.:format) projects#completed
我想根据参数名称检查条件,例如proposed_projects、composed_projects、projects。如果我在form.html.erb? 中做正确的方式?
如果有其他可用的解决方案?
【问题讨论】:
标签: ruby-on-rails