I'm totally confused ... I'm using form_for helper:

 <%= form_for @foods do |f| %> <% end %> 

@foods defined in the controller as:

 def index @foods = Food.all end 

migration is defined as:

 create_table "foods", force: :cascade do |t| t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end 

The model is defined as:

 class Food < ActiveRecord::Base end 

so the rails (gem 'rails', '4.2.6') on ruby ​​(ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]) give:

undefined method `to_key 'for" #Food :: ActiveRecord_Relation: 0x00000002af6f98 "Did you mean? to_query to_ary

What am I doing wrong ???

  • About everything. You made a form for Food.all . What sense did you put into it? - D-side
  • Ay? Are you here? - D-side

1 answer 1

If the form is displayed in the route corresponding to the index method, then the error is that instead of creating one new model

 @food = Food.new 

or search for an existing entry

 @food = Food.find(params[:id]) 

You retrieve all entries that match the model Food

 @foods = Food.all 

You get not the Food object that waits from_for helper, but the ActiveRecord::Relation collection, which the form cannot process. Only one object should be passed to from_for .