form _for usage

Hi all,

I am sure that this is documented somewhere, but I can't find it. I have a class A that has references class B. Like, a.b.name. I was wondering how to represent that with form_for?

<% form_for(@a) do |f| %> <%= f.text_field "b.name" %> <% end %>

That doesn't work, but it kinda shows you what I am talking about.

Any help at all would be appreciated. Thanks!

-fREW

So I'm assuming each instance of class A only references one instance of class B. If not, this answer changes, but if that's the case just let me know.

In the model A.rb:   belongs_to :b

In the model B.rb:   has_many :as # or belongs_to :a if the relationship is one-to-one

In the migration for A:   t.integer :b_id

Then everything should be kosher.

ESPNDev

In the model A.rb: belongs_to :b

In the model B.rb: has_many :as # or belongs_to :a if the relationship is one-to-one

In the migration for A: t.integer :b_id

Well, that's all correct. But I already have the model and migrations defined. I just want to know how to set up a form with form_for to set values for a.b.blah.

-fREW

Oh my bad. You need to use

<%= text_field_tag "b", @a.b.name %>

And then you'll be able to access stuff with params[:b]

Oh my bad. You need to use

<%= text_field_tag "b", @a.b.name %>

And then you'll be able to access stuff with params[:b]

Excellent! This should get me going. Is there any way that I can have the tag formatted the way it is with the rest of the object? For example, in general if my class is Monster with various params I'd have

monster[height], monster[weight], etc with id's being moster_height, monster_weight

And then of course this makes it really easy for me to just do @monster.update_attributes(params[:monster])

Is there a way that I could have the tags for this set to be along those same lines, as follows:

monster[pet][type], monster[pet][name] with monster_pet_type, monster_pet_name

Either way, thanks a lot for the help!

-fREW

wtf?

Whatever happened to using fields_for?

fields_for @a.b do |b| b.text_field “name” end

Well, that works really well, but what if I want that INSIDE of the form_for of @a? Then I get a nested <form> which doesn't make any sense htmlicly speaking.

-fREW

Well, that works really well, but what if I want that INSIDE of the

form_for of @a? Then I get a nested which doesn’t make any

sense htmlicly speaking.

I said fields_for not form_for.

It will work inside the form tag.

Ah, sorry. Thanks much!