我有一个搜索栏表单,该表单根据名称搜索项目。项目按已完成项目,进行中项目和建议项目进行区分。这三个具有相同形式的期望路径名。因此,我决定在助手中使用一种常见形式。
已完成,提案和进行中项目在项目控制器中使用了不同的动作。我尝试用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
中使用正确的方法??
是否还有其他可用的解决方案?
答案 0 :(得分:0)
您的助手没有帮助。如果您要干燥表格,请将其提取为部分内容(例如L
),然后通过_form.html.erb
设置路径:
_form.html.erb
locals
然后,在您的视图中呈现它:
<%= form_tag path, method: :get, :html => {class: "form"} 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 %>
一旦工作,您可以为<% if params[:proposed_projects].present? %>
<%= render 'form', path: proposed_projects_path %>
<% end %>
# add code for completed and in-progress
s编写一个助手:
if
视图
module ProjectsHelper
def form_for_proposed_if(condition)
# render form if condition
end
end
您最终将获得三个可以干燥的助手。
答案 1 :(得分:0)
<%= form_for_projects_search_bar params[:action]=="proposal", params[:action]=="completed", params[:action]=="progress" do %>
<%= text_field_tag :term, params[:term],placeholder:"What are you looking for?", class: "search_field" %>
<% end %>
像这样区分每个动作...这可能对您有所帮助。