Search code examples
ruby-on-rails-4friendly-idruby-2.1globalize

Can't get f.globalize_fields_for to appear using globalize 4.0.2 ruby gem


I'm using rails 4.1.4 and ruby 2.1.2, globalize 4.0.2, batch_translations 0.1.3 (I couldn't get f.globalize_fields_for to work any other way than use this gem), i18n 0.6.11 and friendly_id 5.0.1.

In posts/_form.html.erb, I have f.globalize_fields_for used in two different ways (I'm trying everything) but the f.globalize_fields_for gets hidden in the browser. How do I make it so it doesn't auto hide the

posts/_form.html.erb:

I'm not sure why and how to get it to work.

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.label :post_date %><br>
    <% #= f.date_select :post_date %>
    <%= f.text_field :post_date, :as => :string, :input_html => {:class => 'datepicker col-span-3'} %>
  </div>

  <div class="form-group">
    <%= f.label :image_file %><br>
    <%= f.text_area :image_file %>
  </div>

  <div class="form-group">
    <%= f.label :visible_title %><br>
    <%= f.text_area :visible_title %>
  </div>

  <div class="form-group">
    <%= f.label :meta_title %><br>
    <%= f.text_area :meta_title %>
  </div>

  <div class="form-group">
    <%= f.label :meta_description %><br>
    <%= f.text_area :meta_description %>
  </div>

  <div class="form-group">
    <%= f.label :meta_keywords %><br>
    <%= f.text_area :meta_keywords %>
  </div>

  <div class="form-group">
    <%= f.label :slug %><br>
    <%= f.text_area :slug %>
  </div>

  <div class="form-group">
    <%= f.label :partial_name %><br>
    <%= f.text_area :partial_name %>
  </div>

  <div class="form-group">
    <%= f.label :author %><br>
    <%= f.select :author, authors, { :selected => @post.author }, { :multiple => false} %>
  </div>

  <div class="form-group">
    <%= f.label :category %><br>
    <%= f.select :category, categories, { :selected => @post.category }, { :multiple => false} %>
  </div>

  <div class="form-group">
    <%= f.label :tags %><br>
    <%= f.select :tags, categories, { :selected => @post.tags }, { :multiple => true} %>
  </div>

  <div class="form-group">
    <%= f.label :public %><br>
    <%= f.check_box :public %>
  </div>
  <% [:en, :es, :fr].each do |lang| %>
    <h2><%= lang %> translation</h2>
    <% f.globalize_fields_for lang do |g| %>
      <% [:visible_title, :meta_description, :meta_keywords, :category, :slug, :meta_title].each do |field| %>
        <p><%= g.text_area field %></p>
      <% end %>
    <% end %>
    <hr/>
  <% end %>

  <h2>Spanish translation</h2>
   <% f.globalize_fields_for :es do |g| %>
     <p><%= g.text_field :visible_title %></p>
   <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The above fields get outputted like this in the html:

<h2>es translation</h2>
<input id="post_translations_attributes_2_id" name="post[translations_attributes][2][id]" type="hidden" value="">
<input id="post_translations_attributes_2_locale" name="post[translations_attributes][2][locale]" type="hidden" value="es">

...

<h2>Spanish translation</h2>
<input id="post_translations_attributes_2_id" name="post[translations_attributes][2][id]" type="hidden" value="">
<input id="post_translations_attributes_2_locale" name="post[translations_attributes][2][locale]" type="hidden" value="es">

My Gemfile:

gem "friendly_id", "~> 5.0.1"
gem 'i18n', '~> 0.6.11'
gem 'globalize', '~> 4.0.2'
gem 'batch_translations', '~> 0.1.3' 

Post.rb:

class Post < ActiveRecord::Base
    translates :visible_title, :meta_description, :meta_keywords, :category, :slug, :meta_title

    #puts post.translations.inspect
    # => [#<Post::Translation id: 1, post_id: 1, locale: "en", title: "Globalize rocks!", name: "Globalize">,
      #<Post::Translation id: 2, post_id: 1, locale: "nl", title: '', name: nil>]

    extend FriendlyId 
    friendly_id :slug_candidates, use: [:slugged, :globalize]

    accepts_nested_attributes_for :translations

    # Try building a slug based on the following fields in
    # increasing order of specificity.
    def slug_candidates
      [
        :name
      ]
    end
end

I made a migration and ran it and created this table with the globalization gem. I copied this over from my schema.rb file:

  create_table "post_translations", force: true do |t|
    t.integer  "post_id",          null: false
    t.string   "locale",           null: false
    t.datetime "created_at"
    t.datetime "updated_at"
    t.text     "visible_title"
    t.text     "meta_description"
    t.text     "meta_keywords"
    t.string   "category"
    t.text     "slug"
    t.text     "meta_title"
  end

Solution

  • If I'm right,this line

    <% f.globalize_fields_for lang do |g| %>
    

    should be like this

    <%= f.globalize_fields_for lang do |g| %>
    

    You are missing = symbol