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| %>
<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.
Where did i made a mistake here?
Thanks guys.
--
Guerra
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
> <% 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Like this one: http://railscasts.com/episodes/73
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.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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, On Wed, Jun 11, 2008 at 10:04 AM, Ar Chron <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Like this one: http://railscasts.com/episodes/73 > > 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. > -- > Posted via http://www.ruby-forum.com/. > > > >-- Guerra --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Israel Guerra wrote:> I would like to know where i did a mistake in my relations. >Hrmm... post your current form and controller code? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? On Thu, Jun 12, 2008 at 9:35 AM, Ar Chron <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Israel Guerra wrote: > > I would like to know where i did a mistake in my relations. > > > > Hrmm... post your current form and controller code? > -- > Posted via http://www.ruby-forum.com/. > > > >-- Guerra --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Just found the problem. When i created the migration, the name of the foreign key field was not in plural. Thanks anyway! On Sun, Jun 15, 2008 at 6:54 PM, Israel Guerra <israel.guerra-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? > > > On Thu, Jun 12, 2008 at 9:35 AM, Ar Chron < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> >> Israel Guerra wrote: >> > I would like to know where i did a mistake in my relations. >> > >> >> Hrmm... post your current form and controller code? >> -- >> Posted via http://www.ruby-forum.com/. >> >> >> >> > > > -- > Guerra-- Guerra --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---