Using Globalize gem in nested form using Cocoon gem

192 Views Asked by At

I used both the Globalize gem as well as the Cocoon gem before and everything worked out fine. However, when I try to use Globalize within a cocoon nested form, I get an error message I don't know how to solve.

Context

I use cocoon to manage extra_guest_prices for an extra_guest. As prices depend on the period (e.g. high season is a different price than low season), I use cocoon to create extra_guest_prices for different periods. Everything works well with the cocoon gem upto this point.

Goal Using the globalize gem, I'm trying to insert a specific name in an individual language for a extra_guest_price. The goal is to show a name input field for 3 available languages.

Error Unfortunately, I get the following error messages: browser

no implicit conversion of Integer into String

Extracted source (around line #45):              
      def globalize_fields_for(locale, *args, &proc)
        raise ArgumentError, "Missing block" unless block_given?
        45. @index = @index ? @index + 1 : 1
        object_name = "#{@object_name}[translations_attributes][#{@index}]"
        object = @object.translations.find_by_locale locale.to_s
        @template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")

console

ActionView::Template::Error (no implicit conversion of Integer into String):
     5: 
     6:       <!-- test -->
     7:       <div class="globalize-en globalize" id="en">
     8:         <%= f.globalize_fields_for locale do |ff| %>
     9:           <div class="row">
    10:             <% ff.text_field :locale, as: :hidden, :input_html => { :value => 'en' } %>
    11:             <div class="col-2">

config/initializers/globalization.rb:45:in `+'
config/initializers/globalization.rb:45:in `globalize_fields_for'

Code

models

class ExtraGuest < ApplicationRecord
  belongs_to :age_table
  has_many :extra_guest_prices, dependent: :destroy, inverse_of: :extra_guest
  accepts_nested_attributes_for :extra_guest_prices, allow_destroy: true
  validates :age_table, presence: true
end


class ExtraGuestPrice < ApplicationRecord
  belongs_to :extra_guest
  translates :name, :fallbacks_for_empty_translations => true
  accepts_nested_attributes_for :translations, allow_destroy: true
end

extra_guests_controller

  def new
    @room_category = RoomCategory.find(params[:room_category_id])
    @extra_guest = ExtraGuest.new
    authorize @extra_guest
    @age_table_list = AgeTable.where(park: @room_category.park)
  end

  def extra_guest_params
    params.require(:extra_guest).permit(:name, :age_table_id,
      extra_guest_prices_attributes: [:id, :name, :price_type, :start_date, :end_date, :price, :duration, :duration_min, :duration_max, :backend_only, :weekend_extra, :_destroy,
      translations_attributes: [:id, :_destroy, :locale, :name]])
  end

views/extra_guests/new

<div class="form-container col-12">
  <%= simple_form_for [@room_category, @age_table, @extra_guest] do |f|%>
  <div class="options-form">

  <div class="options-form-item">
    <h4 class="p-3"><%= t('.guest_title') %></h4 class="m-3">
  <div class="row">
    <div class="col col-lg-10">
      <%= f.association :age_table, prompt: t('.placeholder_age_table'), :collection => @age_table_list,value_method: :id, label_method: false %>
    </div>
  </div>
  </div>

  <div class="options-form-item">
  <h4 class="p-3"><%= t('.guest_price_title') %></h4 class="m-3">
    <%= f.simple_fields_for :extra_guest_prices do |price| %>
    <div class="reservation-details">
      <%= render 'extra_guest_price_fields', f: price %>
    </div>
    <% end %>
    <div>
      <%= link_to_add_association f, :extra_guest_prices do %>
      <div class="option-add-option-price">
        <div class="prices-border">
          <i class="fas fa-plus"></i> <%= t('.add_period') %>
        </div>
      </div>
      <% end %>
    </div>
    <div class="row">
      <div class="col col-lg-6"> <%= f.button :submit, t('.save_price_button'), class: "create-reservation-btn"%>
      </div>
    </div>
    <% end %>
  </div>
</div>
</div>
</div>

views/extra_guest_price_fields

<div class="nested-fields border-bottom">
    <div class="row">
<!-- test -->
      <div class="globalize-en globalize" id="en">
        <%= f.globalize_fields_for 'en' do |ff| %>
          <div class="row">
            <% ff.text_field :locale, as: :hidden, :input_html => { :value => 'en' } %>
            <div class="col-2">
              <%= flag_icon(:gb) %>
            </div>
            <div class="col-10">
              <div class="form-group">
                <label class="form-control-label text required" for="room_category_description">Name</label>
                <div><%= ff.text_field :name, :rows =>"1",  class:"form-control is-valid text required" %>
                </div>
              </div>
            </div>
          </div>

          <% end %>
        </div>

      <!-- test -->
      <div class="globalize-nl globalize" id="nl">
        <%= f.globalize_fields_for 'nl' do |ff| %>
          <div class="row">
            <% ff.text_field :locale, as: :hidden, :input_html => { :value => 'nl' } %>
            <div class="col-2">
              <%= flag_icon(:nl) %>
            </div>
            <div class="col-10">
              <div class="form-group">
                <label class="form-control-label text required" for="room_category_description">Name</label>
                <div><%= ff.text_field :name, :rows =>"1",  class:"form-control is-valid text required" %>
                </div>
              </div>
            </div>
          </div>

          <% end %>
        </div>

      <!-- test -->
      <div class="globalize-fr globalize" id="fr">
        <%= f.globalize_fields_for 'fr' do |ff| %>
          <div class="row">
            <% ff.text_field :locale, as: :hidden, :input_html => { :value => 'fr' } %>
            <div class="col-2">
              <%= flag_icon(:fr) %>
            </div>
            <div class="col-10">
              <div class="form-group">
                <label class="form-control-label text required" for="room_category_description">Name</label>
                <div><%= ff.text_field :name, :rows =>"1",  class:"form-control is-valid text required" %>
                </div>
              </div>
            </div>
          </div>
          <% end %>
        </div>
        <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 option-price-delete">
      <%= link_to_remove_association f do %>
      <i class="fas fa-trash"><%= t('.delete_price') %></i>
      <% end %>
    </div>
      </div>
    </div>
      ....

initializer/globalization.rb

module ActionView
  module Helpers
    class FormBuilder
          def globalize_fields_for(locale, *args, &proc)
        raise ArgumentError, "Missing block" unless block_given?
        @index = @index ? @index + 1 : 1
        object_name = "#{@object_name}[translations_attributes][#{@index}]"
        object = @object.translations.find_by_locale locale.to_s
        @template.concat @template.hidden_field_tag("#{object_name}[id]", object ? object.id : "")
        @template.concat @template.hidden_field_tag("#{object_name}[locale]", locale)
        @template.fields_for(object_name, object, *args, &proc)
      end
    end
  end
end
0

There are 0 best solutions below