1

I have a form ;

<%= form_for @boats do |f| %>

<%= f.collection_select(:brand, :brand_id,  @brands,  :id, :name, {:prompt   => "Select a Brand"}, {:id => 'brands_select'}) %>

<%= f.collection_select(:year, :year_id, @years, :id, :name, {:prompt   => "Select a Year"}, {:id => 'years_select'}) %>

<%= f.collection_select(:model, :model_id, @models, :id, :name, {:prompt   => "Select a Model"}, {:id => 'models_select'}) %>
<%= f.submit "Create my account" %>

    <% end %> 

and have controller #index;

def index
    @boats = Boat.new
    @brands  = Brand.all
    @years = Year.all
    @models   = Model.all
  end

But the problem here is that, when I run the code it gives an error of; enter image description here

So I am not sure what to do. Basically, the data comes from the databases and I would like to save them to Boat database where the column names are Brand, Year and Model.

Shalafister
  • 401
  • 2
  • 6
  • 21
  • 2
    the last 2 arguments you are giving are 2 distincts hash, instead of only one. Use `{:prompt => "Select a Brand", :id => 'brands_select'}` instead. – MrYoshiji Apr 15 '15 at 15:09
  • Thank you @MrYoshiji. But now it gives another error: `NoMethodError in HomeController#index` , `undefined method merge' for :name:Symbol` – Shalafister Apr 15 '15 at 16:44

1 Answers1

4

The correct order of arguments is:

method, collection, value_method, text_method, options = {}, html_options = {}

I.e.:

<%= f.collection_select :brand_id, @brands, :id, :name, {prompt: 'Select a Brand'}, {id: 'brand_select'} %>
Andrey Turkin
  • 719
  • 4
  • 12
  • But this time it saves with the ids, not the names; SELECT "boats".* FROM "boats" ORDER BY "boats"."id" ASC LIMIT 1 => # – Shalafister Apr 15 '15 at 19:22
  • Look, you don't need to store that names in record for boat. Keep them into tables for appropriate models (Brand, Model, etc.) which you can reference by primary key (id). That's what relational databases and ActiveRecord are designed for. You can use following syntax to get boat brand name: `boat.brand.name`. – Andrey Turkin Apr 16 '15 at 03:57