Hi ppl.
I am noob in rails and started an app to learn. I have the following scenario:
table client with fileds: name, second_name
table phone with fields: number, client_id
table address with fields: street, number, client_id
The migrations worked ok and the fields are created.
Now i defined in the model that
for client:
has_many :phones
has_many :adresses
for phones and adresses:
belongs_to :client
I made a form to get some of these fields and add to the database:
<% form_for(:clients) do |f| %>
Name
<%= f.text_field :name %>
Second Name
<%= f.text_field :second_name%>
Number
<%= f.text_field :number%>
<%= f.submit "Add" %>
<% end %>
And in the controller i try to save it to the DB:
@clients = Clients.new(params[:clients])
@clients.save
When
i submit i get an error saying that number is not a method, it means it
didnt associate number with a client. It’s not “joining” the tables.
Where did i made a mistake here?
Thanks guys.
11175
(-- --)
June 11, 2008, 12:55pm
2
<% form_for(:clients) do |f| %>
<p>
<b>Name</b>
<%= f.text_field :name %>
</p>
<p>
<b>Second Name</b>
<%= f.text_field :second_name%>
</p>
<p>
<b>Number</b>
<%= f.text_field :number%>
</p>
<p>
<%= f.submit "Add" %>
</p>
<% end %>
And in the controller i try to save it to the DB:
@clients = Clients.new(params[:clients])
@clients.save
When i submit i get an error saying that number is not a method, it
means it
didnt associate number with a client. It's not "joining" the tables.
The way your form is designed, rails is expecting the :number field to
reside in the model you're presenting on your form (@client ), which it
isn't, it's in an associated model. There are some good railscasts out
there about "complex forms" (i.e., forms using fields from more than one
model).
Go forth and google grasshopper.
11175
(-- --)
June 11, 2008, 1:04pm
3
Like this one: #73 Complex Forms Part 1 - RailsCasts
And just cause it caught my eye (of course after I posted that last
comment):
Is your table name is clients, or client? (Tables are generally plural
in rails)
If it's a form for a single client, wouldn't that be
form_for(:client) do |f|
an Add is usually for a single entity, and the controller would use
def create
@client = Client.new(params[:client])
if @client.save
blah blah blah
when just picking up rails, it is often easier to follow the rails
mindset with regards to naming. It makes life much easier.
Hi Ar, thanks for helping
I think im using the correct names.
I watcher the cool railcasts and managed to create the form to get more than one number per user for instance.
The problem is that when i check the database, it doesnt create a foreign key for the numbers i add.
It simply adds null in the foreign key fields.
I would like to know where i did a mistake in my relations.
Thanks,
11175
(-- --)
June 12, 2008, 12:35pm
5
Israel Guerra wrote:
I would like to know where i did a mistake in my relations.
Hrmm... post your current form and controller code?
Here are my 4 files.
I have the clientes, cliente_telefone and cliente_endereco models.
clientes has_many cliente_telefones and cliente_enderecos. and each of these (telefones and enderecos) belongs to telefone.
In the controller for clientes i create a form for 3 telefones for a cliente. After i run it, the cliente_telefone data is in the DB, but with the foreign key fields with NULL.
How can i make rails create it correctlly?
clientes_controller.rb (430 Bytes)
cliente_endereco.rb (69 Bytes)
cliente_telefone.rb (69 Bytes)
clientes.rb (244 Bytes)
Just found the problem.
When i created the migration, the name of the foreign key field was not in plural.
Thanks anyway!