@model OR :model in a form_for

Hi Folks,

I want a form in a rails 3.0.10 app with form_for exactly I have 3
models

@apple, @basket, @table

class Apple
    belongs_to :tables
    belongs_to: baskets
end

class Basket
    belongs_to :tables
    has_many :apples
end

class Table
    has_many :baskets
    has_many :apples
end

MY QUESTION

For apples I have many color as well as for baskets I have many
different kinds of shapes

if I have the follow code in the index.html.erb file

<%= form_for :table do |f| %>

<%= f.collection_select(:basket, @basket, :id, :shape, {:prompt =>
true}) %>

<%= f.collection_select(:apple, @apple, :id, :color, {:prompt => true})
%>

<%= f.submit %>

<% end %>

*** appear the page correctly ***

meanwhile if I have the follow code

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

<%= f.collection_select(:basket, @basket, :id, :shape, {:prompt =>
true}) %>

<%= f.collection_select(:apple, @apple, :id, :color, {:prompt => true})
%>

<%= f.submit %>

<% end %>

the page show the follow error page

////////////
undefined method `model_name' for NilClass:Class

Extracted source (around line #1):

1: <%= form_for @table do |f|
////////////

change only the first line in the example I have <%= form_for @table do

f> %> and not

<%= form_for :table do |f| %>

NOW

which is the different between

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

and

<%= form_for :table do |f| %>

sorry for this post but I want to understand the Rails philosophy

thanks a lot anyway,

C

Is it all made with scaffolds? or what do you have in your controller?

Javier Q.

Hi Folks,

I want a form in a rails 3.0.10 app with form_for exactly I have 3
models

@apple, @basket, @table

class Apple
belongs_to :tables
belongs_to: baskets
end

class Basket
belongs_to :tables
has_many :apples
end

class Table
has_many :baskets
has_many :apples
end

MY QUESTION

For apples I have many color as well as for baskets I have many
different kinds of shapes

if I have the follow code in the index.html.erb file

<%= form_for :table do |f| %>

<%= f.collection_select(:basket, @basket, :id, :shape, {:prompt =>
true}) %>

<%= f.collection_select(:apple, @apple, :id, :color, {:prompt => true})
%>

<%= f.submit %>

<% end %>

*** appear the page correctly ***

meanwhile if I have the follow code

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

<%= f.collection_select(:basket, @basket, :id, :shape, {:prompt =>
true}) %>

<%= f.collection_select(:apple, @apple, :id, :color, {:prompt => true})
%>

<%= f.submit %>

<% end %>

the page show the follow error page

////////////
undefined method `model_name' for NilClass:Class

That means that @table is nil. This form cannot work if @table has
not been setup to be an object of appropriate type. :table is a
symbol used here to represent a type of object, @table is a variable
that must have been setup in the controller in order to use it in the
view.

Colin.

Colin

Thanks for all answer I don't use the scaffold for that app

here is my table CONTROLLER

class TableController < ApplicationController

  def index
    @basket = Basket.find(:all)
    @apple = Apple.find(:all)
  end

  def new
    @table = Table.new
  end
end

here my MODEL

class Table < ActiveRecord::Base

belongs_to :apples
belongs_to :baskets

end

maybe above you can see a logic error? or syntax error?

thanks anyway,

C

Colin Law wrote in post #1052589:

If you are going to use @table in index.html.erb (that is handled by def index ) you have to add to the index method

def index

@table = Table.new

end

but I don’t know why does it works with the symbol and not with the instance

Javier Q.

Hi Javier,

you are right I just added it anyway when I reloaded the page now I have
another error

undefined method `basket' for #<Table id: nil, created_at: nil,
updated_at: nil>

I edit the basket and apple controller with

def index

@basket = Basket.find(:all)

end

def index

@apple = Apple.find(:all)

end

but I have always the same error

thanks

C

Javier Quarite wrote in post #1052696:

I’m not sure what are the fields of your Table … table :D, but according with your error it only has and id, and the timestamps(created_at, update_at)

If you want to save inside Table the basket_id and the color_id then you have to add columns for it, after that instead of the symbols :basket and :apple, write :basket_id and :apple_id (I’m not sure if thats correc because I don’t usually use the form helpers from rails, I use a gem simple_form).

You should take a look rails guides on views and forms

Javier Q.

Hi,

I figured out this error

undefined method `basket' for #<Table id: nil, created_at: nil,
updated_at: nil>

simply add two columns (apple & basket) in the table called Table :slight_smile:

now appear correctly the webpage with the drop down menu so

If you have in the app/views/table/new.html.erb

1. <%= form_for @table do |f| %>
2. <%= f.collection_select(:basket, @basket, :id, :shape, {:prompt =>
true}) %>
3. <%= f.collection_select(:apple, @apple, :id, :color, {:prompt =>
true}) %>

it works but

A. in the model Table you have the follow code

def new

@apple = Apple.find(:all)
@Basket = Basket.find(:all)
@table = Table.new

end

B. in the table called 'Table' obviously you have 2 record 'apple' and
''

NOW

even if I don't have the right code as letter A. and letter B. if I
write in the line 1.

<%= form_for :table do |f| %>

and not

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

the page appear correctly and load also the data from apple & basket
table in the drop down menu

I don't know why but is only a doubt to understand the difference
between a symbol and an instance

thanks a lot anyway

C