1

There is a cucumber feature in my Rails application. There is a step formulated to fill out a form field within a table which is within :accepts_nested_attributes scope of the model (The example here is basically an ItemSupplier has many Items for different prices).

The form visually looks like this:

enter image description here

The relevant part of the cucumber feature looks like this:

...
When I fill in "Name des Lieferanten" with "any_name"
  And I fill in "Straße & Hausnummer" with "SBS 62"
  And I fill in "Postleitzahl" with "12345"
  And I fill in "Stadt" with "Bonn"
  And I fill in "item_supplier[supplier_item_prices_attributes][0][price]" with "1,23"
...

While the above part of filling out of the form looks ok I addressed the price field of the item in line 5 of above code in a very technical way to make it work (And I fill in item_supplier[supplier_item_prices_attributes][0][price])

Relevant form code as .erb:

<%= simple_form_for @item_supplier, url: path do |f| %>
  <div class="row" xmlns="http://www.w3.org/1999/html">
    <div class="col-6">
      <div class="ibox float-e-margins">
        <div class="ibox-content">
          <% if @item_supplier.errors.any? %>
            <div id="error_explanation">
              <h4>
                <%= t('activerecord.errors.models.item_supplier.prohibited_save', count: @item_supplier.errors.count) %>
              </h4>
              <ul>
                <% @item_supplier.errors.full_messages.each do |message| %>
                  <li><%= message %></li>
                <% end %>
              </ul>
            </div>
          <% end %>
          <div class="form-group">
            <div class="col-lg-8">
              <%= f.input :name %>
            </div>
          </div>
          <div class="form-group">
            <div class="col-lg-8">
              <%= f.input :street_and_no %>
            </div>
          </div>
          <div class="form-group">
            <div class="col-lg-8">
              <%= f.input :postal_code %>
            </div>
          </div>
          <div class="form-group">
            <div class="col-lg-8">
              <%= f.input :city %>
            </div>
          </div>
          <table class="table table-responsive table-bordered">
            <tr>
              <th>
                <%= t('activerecord.attributes.item.name') %>
              </th>
              <th><%= t('common.general.price_per_unit') %></th>
            </tr>
            <% sorted_items = @item_supplier.supplier_item_prices.sort_by{|supplier_item_price| supplier_item_price.item.name} %>
            <%= f.fields_for :supplier_item_prices, sorted_items do |ff| %>
              <tr>
                <%= ff.hidden_field :id %>
                <%= ff.hidden_field :item_id, value: ff.object.item_id %>
                <td><%= ff.object.item.name %></td>
                <td><%= ff.text_field :price, label: false %></td>
              </tr>
            <% end %>
          </table>
          <hr>
          <% button_title = t('item_supplier.form.button_submit.new') if action == :new %>
          <% button_title = t('item_supplier.form.button_submit.edit') if action == :edit %>
          <%= f.submit button_title, class: 'btn btn-primary' %>
        </div>
      </div>
    </div>
  </div>
<% end %>

My question

Is there no "human readable" way to adress fields in a table in cucumber like in this example?

4
  • 1
    You can create your own step definitions so maybe something like /I fill in the {attribute} for row {row_number} of the {base_object} {sub_object} table with {amount}/ called as "I fill in the price for row 1 of the item_supplier supplier_item_prices table with 1.23". Then in the definition body you can assemble as fill_in "#{base_object}[#{sub_object}_attributes][#{row_number.to_i - 1}][#{attribute}]", with: amount Commented Jul 22 at 19:37
  • 2
    In addition to @engineersmnky's suggestion I would consider using a <fieldset> tag instead of tables within tables like it's 1999. It will make it way easier to target the elements. Commented Jul 23 at 9:39
  • @engineersmnky I understand I have to program something custom. Hoped there is something out of the "magic box" I dont know Commented Jul 23 at 22:54
  • @max first time in my life I am hearing "fieldset". Have to check this out. How to make rows for each nested-object and so. But thanks for the tip! Commented Jul 23 at 22:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.