1

I'm trying to make an app with Rails 4. I use simple form.

I have a form asking users to pick their working language.

 <%= f.input :working_languages, as: :select, collection: AVAILABLE_LANGUAGES.sort.map {|k,v| [v,k]}, label: "Select your working language" %>

I also have an available_languages.rb in my config/initializer folder

AVAILABLE_LANGUAGES = {
    en: "English", 
    de: "Deutsche", 
    fr: "Français", 
    es: "Español"
}

When I try that, I get this error. Does anyone know what it means?

NameError in Profiles#edit
profiles/_form.html.erb where line #39 raised:

uninitialized constant ActionView::CompiledTemplates::AVAILABLE_LANGUAGES
2
  • can you please paste complete code for your available_languages.rb Commented Jan 15, 2016 at 10:44
  • That's the entire file Commented Jan 15, 2016 at 10:45

2 Answers 2

3

config/initializers/language_array.rb

module LanguageArray
  AVAILABLE_LANGUAGES = {
    en: "English", 
    de: "Deutsche", 
    fr: "Français", 
    es: "Español"
}
end

and access in your form as below:

<%= f.input :working_languages, as: :select, collection: LanguageArray::AVAILABLE_LANGUAGES.sort.map {|k,v| [v,k]}, label: "Select your working language" %>
Sign up to request clarification or add additional context in comments.

5 Comments

uninitialized constant ActionView::CompiledTemplates::LanguageArray
Have you restarted the server?
renaming the initialiser fixed it, except <%= @profile.working_languages.titlecase %> outputs the 'en' instead of the 'english'
<%= LanguageArray::AVAILABLE_LANGUAGES[@profile.working_languages].to_sym %> try this
try this <%= LanguageArray::AVAILABLE_LANGUAGES[@profile.working_languages.to_sym] %>
0

A better way will be to put that in a helper & use this for your constant (which is available in your views):

#config/initializers/global_constants.rb
module GlobalConstants
    # also notice the call to 'freeze'
    LANGUAGES = {
       en: "English", 
       de: "Deutsche", 
       fr: "Français", 
       es: "Español"
    }.freeze
end

#app/helpers/application_helper.rb
module ApplicationHelper
   def available_languages
       GlobalConstants::LANGUAGES
   end
end

You'd then be able to use:

<%= f.collection_select :working_languages, available_languages, :first, :last, label: "Select your working language" %>

4 Comments

Hi Rich, I'm afraid I'm out of guesses as to why this is better. I think you might be much further advanced in your skills than I'm able to keep pace with. Having read this: rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/…, I'm not sure why I'd want to freeze the user's language option. I'm afraid, I can't think what the advantage of this approach might be. Thanks anyway - I'll go with what I've got.
You're freezing the constant. I only took that idea from the referenced answer; I thought it was nice. A constant can't be changed, variables can. Thus if you're going to declare a constant, you can't change it anyway.
Really not sure what that means. I think you're too advanced for me to try to understand this. I don't mind if users change their selected languages, if that's what you're referring to. Thanks anyway.
Constants and variables are base components of programming. You declared AVAILABLE_LANGUAGES as a constant. Constants don't change, variables can. With your select form, you're changing the variable working_languages by picking from the constant

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.