Refaktor bootstrapowego formularza

Jak byście się za to zabrali?

<%= form_for @event, html: {class: "form-horizontal"} do |f| %>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :facebook %>">
    <%= f.label :facebook, "Facebook Event Link", class: "col-sm-2 control-label" %>
    <div class="col-sm-10">
      <div class="input-group">
        <%= f.text_field :facebook, placeholder: "http://...", class: "form-control" %>
        <span class="input-group-btn">
          <button class="btn btn-primary" type="button">Import Details</button>
        </span>
      </div>
      <% if @event.errors.has_key? :facebook %>
      <span class="help-block">
        <%= @event.errors.full_messages_for( :facebook ).first %>
      </span>
      <% end %>
    </div>
  </div>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :name %>">
    <%= f.label :name, class: "col-sm-2 control-label" %>
    <div class="col-sm-10">
      <%= f.text_field :name, class: "form-control" %>
      <% if @event.errors.has_key? :name %>
        <span class="help-block">
          <%= @event.errors.full_messages_for( :name ).first %>
        </span>
      <% end %>
    </div>
  </div>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :start_date %>">
    <%= f.label :start_date, "Start Date", class: "col-sm-2 control-label" %>
    <div class="col-sm-2">
      <%= f.date_field :start_date, class: "form-control" %>
    </div>
    <% if @event.errors.has_key? :start_date %>
    <div class="col-sm-8">
      <span class="help-block">
        <%= @event.errors.full_messages_for( :start_date ).first %>
      </span>
    </div>
    <% end %>
  </div>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :end_date %>">
    <%= f.label :end_date, "End Date", class: "col-sm-2 control-label" %>
    <div class="col-sm-2">
      <%= f.date_field :end_date, class: "form-control" %>
    </div>
    <% if @event.errors.has_key? :end_date %>
    <div class="col-sm-8">
      <span class="help-block">
        <%= @event.errors.full_messages_for( :end_date ).first %>
      </span>
    </div>
    <% end %>
  </div>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :venue %>">
    <%= f.label :venue, class: "col-sm-2 control-label" %>
    <div class="col-sm-10">
      <%= f.text_field :venue, class: "form-control" %>
      <% if @event.errors.has_key? :name %>
        <span class="help-block">
          <%= @event.errors.full_messages_for( :venue ).first %>
        </span>
      <% end %>
    </div>
  </div>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :city %>">
    <%= f.label :city, class: "col-sm-2 control-label" %>
    <div class="col-sm-10">
      <%= f.text_field :city, class: "form-control" %>
      <% if @event.errors.has_key? :name %>
        <span class="help-block">
          <%= @event.errors.full_messages_for( :city ).first %>
        </span>
      <% end %>
    </div>
  </div>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :logo %>">
    <%= f.label :logo, class: "col-sm-2 control-label" %>
    <div class="col-sm-10">
      <%= f.url_field :logo, placeholder: "http://...", class: "form-control" %>
      <% if @event.errors.has_key? :name %>
        <span class="help-block">
          <%= @event.errors.full_messages_for( :logo ).first %>
        </span>
      <% end %>
    </div>
  </div>
  <div class="form-group <%= "has-error" if @event.errors.has_key? :website %>">
    <%= f.label :website, "Website URL", class: "col-sm-2 control-label" %>
    <div class="col-sm-10">
      <%= f.url_field :website, placeholder: "http://...", class: "form-control" %>
      <% if @event.errors.has_key? :website %>
        <span class="help-block">
          <%= @event.errors.full_messages_for( :venue ).first %>
        </span>
      <% end %>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <%= f.submit "Submit Event", class: "btn btn-primary" %>
    </div>
  </div>
<% end %>

Może spróbuj tego? :wink:


Ewentualnie http://haml.info/ gdyby slim cię przerażał.

Zmiana z erb na haml czy slim niewiele tu zmieni tak naprawdę. Ja osobiście preferuję erb choć długo uważałem, że haml jest super i wspaniały ale przy dużej ilości wcięć powoduje nerwice i inne choroby :wink:

Najwięcej zyskasz jak:

1 - Wywalisz bloki błędów do partiala. Przykład:

   <% if @event.errors.has_key? :end_date %>
    <div class="col-sm-8">
      <span class="help-block">
        <%= @event.errors.full_messages_for( :end_date ).first %>
      </span>
    </div>
    <% end %>

zmień na

<%= render ‘errors’, name: : end_date, event: @event %>

Partial:

   <% if event.errors.has_key? name %>
    <div class="col-sm-8">
      <span class="help-block">
        <%= event.errors.full_messages_for( name ).first %>
      </span>
    </div>
    <% end %>

2 - Zastosuj helpery dla prosty metod np.

class=“form-group <%= “has-error” if @event.errors.has_key? :name %>”

zmień na :

class=“form-group <%= has_error(@event, :name) %>”>