1

I got an error while trying to use collection_select ;)

I got this code in my view:

<%= f.collection_select(:channel, :channel_id, @channels, :id, :channelname, prompt: true) %>

in my controller I have this:

    @channels = Channel.all

and I got this error:

undefined method `merge' for :channelname:Symbol

Whats my failure ?

Thanks at all!

Felix
  • 5,452
  • 12
  • 68
  • 163
  • possible duplicate of http://stackoverflow.com/questions/8147069/collection-select-method-gives-error-in-rails-3-1-1 or http://stackoverflow.com/questions/27136478/undefined-method-merge-for-namesymbol-rails-4-1-x-collection-select – margo Apr 20 '16 at 12:03

3 Answers3

1

You can use like

Channel.all.pluck(:id, :channelname)

For example take look at below

collection_select(
    :post, # field namespace 
    :author_id, # field name
    # result of these two params will be: <select name="post[author_id]">...

    # then you should specify some collection or array of rows.
    # It can be Author.where(..).order(..) or something like that. 
    # In your example it is:
    Author.all, 

    # then you should specify methods for generating options
    :id, # this is name of method that will be called for every row, result will be set as key
    :name_with_initial, # this is name of method that will be called for every row, result will be set as value

    # as a result, every option will be generated by the following rule: 
    # <option value=#{author.id}>#{author.name_with_initial}</option>
    # 'author' is an element in the collection or array

    :prompt => true # then you can specify some params. You can find them in the docs.
)
Bharat soni
  • 2,686
  • 18
  • 27
  • This doesn't make use of the form builder object which means any existing values wont automatically be preselected by rails when the view gets rendered. – hypern Apr 20 '16 at 13:04
1

According to the docs:

collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) public

So therefore you should use:

<%= f.collection_select(:channel_id, Channel.all, :id, :channelname, prompt: true) %>
hypern
  • 887
  • 4
  • 16
  • whats the difference between my one, my @channels should have the collection or not? – Felix Apr 20 '16 at 13:05
  • 1
    There is no difference. You can use @channels as per your example. – hypern Apr 20 '16 at 13:06
  • additional question, why is this not working: {prompt: t ("channel.add.prompt")} – Felix Apr 20 '16 at 13:27
  • prompt expects a string value or a boolean. What are you trying to do? – hypern Apr 20 '16 at 13:31
  • I want to load a string from my locales for translating – Felix Apr 20 '16 at 13:44
  • To use the translation helper method you need to have a translation defined in your locales folder and pass its key into the translation helper method, such as t(:your_defined_translation). [See here](http://guides.rubyonrails.org/i18n.html#adding-translations) – hypern Apr 20 '16 at 14:11
  • yes of course, I did ... have channel.add.prompt in my locales. Error is: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '(' ...:id, :channelname, {prompt: t "channel.add.prompt"}, class: ... ... – Felix Apr 20 '16 at 14:20
0

try this

<%= f.collection_select(:channel_id, :id, prompt: true) %>