krunal shah
krunal shah

Reputation: 16339

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 %>

Upvotes: 0

Views: 151

Answers (6)

mpapis
mpapis

Reputation: 53178

<% %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 %>

Upvotes: 5

fl00r
fl00r

Reputation: 83680

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

Upvotes: 1

Matt
Matt

Reputation: 5398

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 %>

Upvotes: 1

buru
buru

Reputation: 3210

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 %>

Upvotes: 1

Ashish
Ashish

Reputation: 5791

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?}

Upvotes: 1

Chowlett
Chowlett

Reputation: 46675

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.

Upvotes: 1

Related Questions