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
Mel
  • 2,481
  • 26
  • 113
  • 273

2 Answers2

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" %>
Vrushali Pawar
  • 3,753
  • 1
  • 13
  • 22
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" %>
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 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: https://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/39-ruby-s-object-model/lessons/132-cloning-and-freezing-objects, 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. – Mel Jan 15 '16 at 22:47
  • 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. – Richard Peck Jan 16 '16 at 08:54
  • 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. – Mel Jan 16 '16 at 08:57
  • [Constants and variables](https://msdn.microsoft.com/en-us/library/wew5ytx4(v=vs.90).aspx) 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 – Richard Peck Jan 16 '16 at 08:59