开发者

Are there any better ways to write this code?

      <% unless params[:date_from].blank? %>
        <input type="hidden" name="date_from" value="<%= params[:date_from] %>"/>
      <% end %>
      <% unless params[:date_to].blank? %>
        <input type="hidden" name="date_to" value="<%= params[:date_to] %>"/>
      <% end %>
      <% unless params[:from_hh].blank? %>
        <input type="hidden" name="from_hh" value="<%= params[:from_hh] %>"/>
      <% end %>
      <% unless params[:from_mm].blank? %>
        <input type="hidden" name="from_mm" value="<%= params[:from_mm] %>"/>
      <% end %>
      <% unless params[:from_ampm].blank? %>
        <input type="hidden" name="from_ampm" value="<%= params[:from_ampm] %开发者_高级运维>"/>
      <% end %>
      <% unless params[:to_hh].blank? %>
        <input type="hidden" name="to_hh" value="<%= params[:to_hh] %>"/>
      <% end %>
      <% unless params[:to_mm].blank? %>
        <input type="hidden" name="to_mm" value="<%= params[:to_mm] %>"/>
      <% end %>
      <% unless params[:to_ampm].blank? %>
        <input type="hidden" name="to_ampm" value="<%= params[:to_ampm] %>"/>
      <% end %>


<% %w(date_from date_to from_hh from_mm from_ampm to_hh to_mm to_ampm).
  reject{|field| params[field].blank? }.each do |field| %>
  <%= hidden_field_tag field, params[field] %>
<% end %>


you can use @mpapis approach, that is quite fit your question with little improvement:

<% [:date_from, :date_to, :from_hh, :from_mm, :from_ampm, :to_hh, :to_mm, :to_ampm].select{|field| params[field].present? }.each do |field| %>
  <%= hidden_field_tag field, params[field] %>
<% end %>

But if fields are different but you still need to check blank? for params you can use form helpers with statement:

# Before
<% unless params[:date_from].blank? %>
  <input type="hidden" name="date_from" value="<%= params[:date_from] %>"/>
<% end %>

# After
<%= hidden_field_tag :date_form, params[:date_form] if params[:date_form].present? %>

So now you can combine all kind of form elements:

<%= text_field_tag :date_form, params[:date_form] if params[:date_form].present? %>
<%= select_tag :date_form, params[:date_form] if params[:date_form].present? %>

etc


Yes. I'd go for:

<% [:date_from, :date_to, :from_hh, :from_mm, :from_ampm, :to_hh, :to_mm, :to_ampm].each do |field| %>
  <% unless params[field].blank? %>
    <input type="hidden" name="<%= field.to_s %>" value="<%= params[field] %>"/>
  <% end %>
<% end %>

Edit: Missed the "blank" constraint.


I think these checks are not required. Just use all input fields without any check.

 <% [:date_from, :date_to, :from_hh, :from_mm, :from_ampm, :to_hh, :to_mm, :to_ampm].each   
 do |field| %>
 <input type="hidden" name="<%= field.to_s %>" value="<%= params[field] %>"/>
 <% end %>

In your controller you can have a check if needed

  params.reject{|k,v| v.blank?}


In the controller:

@fields = ["date_from","date_to","from_hh","from_mm","from_ampm","to_hh","to_mm","to_ampm"]

In the view:

<% @fields.each do |f| %>
  <% unless params[f].blank? %>
    <input type="hidden" name="<%= f %>" value="<%= params[f] %>"/>
  <% end %>
<% end %>


How about this:

<% [:date_from, :date_to, :from_hh, :from_mm, :from_ampm, :to_hh, :to_mm, :to_ampm].each do |field| %>
  <%= hidden_field_tag field.to_s, params[field] unless params[field].blank? %>
<% end %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜