Search code examples
ruby-on-railssimple-form

Rails - language selector


I am trying to make an app with Rails 4. I am using simple form and bootstrap.

I am trying to ask users which language they work in.

I have an attribute in my profiles table to store the language value.

I am trying to follow this advice for making the selector: interface language selector in rails form?

I am finding Simple Form anything but simple and am not having any success in following the example shown in answer to the question in the above link.

In my profile.rb, I have:

def.self.language_list
    [['English', 'en'], ['French', 'fr'], ['German', 'gmh'], ['Spanish', 'spa']]
  end

In my profile view form, I have:

<div class="col-md-3 col-md-offset-1">
        <%= f.label  'What are your working languages?',   :class => 'question-project' %>
      </div>
      <div class="col-md-7">
      <%= f.input :languages do %>
        <%= f.select :languages, options_for_select(Profile.language_list, @profile.languages) %> 
        <% end %> 
      </div>
    <

When I try this, I get an error which says:

NoMethodError at /profiles/2/edit
undefined method `language_list' for #<Class:0x007fe53323d078>

I have the same issue when I try the collection select alternative outlined in the answer to the above post.

Does anyone see what I've done wrong? Is there an easier way to put a list of languages into a form?


Solution

  • You probably need to write

    def self.language_list
      [['English', 'en'], ['French', 'fr'], ['German', 'gmh'], ['Spanish', 'spa']]
    end
    

    instead

    def.self.language_list
        [['English', 'en'], ['French', 'fr'], ['German', 'gmh'], ['Spanish', 'spa']]
      end
    

    Dot between def and self.language_list is unwanted