Topic: Help with Refactoring into partial?

I'm looking to refactor this into a partial as I essentially have the same code 7 times....  any suggestions would be appreciated.

<%="<div class=\"monday\">
  <div class =\"open_hours\">
    <li><label for=\"location_monday\">Monday</label>
        Open: #{h @location.monday_open.strftime("%I:%M%p")} - 
        Close: #{h @location.monday_close.strftime("%I:%M%p")} 
  #{link_to_function "remove", "remove_fields(this)"}</li>
    <div class=\"hidden_times\">
        #{f.input :monday_open, :as => :hidden}
        #{f.input :monday_close, :as => :hidden}
  </div>
  </div>  
</div>" if @location.monday_open?%>  

Thanks!

Last edited by NickFaraday (2010-02-04 14:14:25)

Re: Help with Refactoring into partial?

Yup, could use some refactoring smile   You can use collection to your partial and send for your methods.
Should work, not tested though.

<% days = %w[sunday monday tuesday wednesday thursday friday saturday] %>
<%= render :partial => 'day', :collection => days %>

The partial (_day.html.erb):

<% if @location.send("#{day}_open?") %>
  <div class="<%= day %>">
    <div class="open_hours">
      <li>
        <label for="location_<%= day %>"><%= day.camelize %></label>
        Open: <%= h @location.send("#{day}_open").strftime("%I:%M%p") %> -
        Close: <%= h @location.send("#{day}_close").strftime("%I:%M%p") %>
        <%= link_to_function "remove", "remove_fields(this)" %>
      </li>
      <div class="hidden_times">
          <%= f.input "#{day}_open", :as => :hidden %>
          <%= f.input "#{day}_close", :as => :hidden %>
      </div>
    </div>   
  </div>
<% end %>

Re: Help with Refactoring into partial?

Awesome! This is exactly what I was looking for....

I was unaware of the send method...

Only needed one minor change to get it to work. Need to pass the form variable as a local...

<%= render :partial => 'day', :locals => {:f => f}, :collection => days %>

Thanks again!