Forum: Ruby on Rails Creating a new object with a passed in parent?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Duane (Guest)
on 2006-02-07 14:49
I have a simple object, Article, which has_one :parent of the same time.
On my Show page for a given article there will be a "new child" button
which will pass the current :id (or the current Article itself) to the
new form.  What is the proper way to hide a reference to this parent in
the form so that when it is submitted back, the create method knows what
to do with it?

I'm confused by where Rails is going to make assumptions for me.  Does
article have a property called parent, or parent_id?  When I set it do I
just pass in an id and Rails knows what to do with it, or do I have to
create the object myself and then set it?  I tried something like this:

<%= hidden_field 'article', 'parent', @parent %>

where @parent had been defined in the new method as
  @parent = Article.find(params[:id])

but I got undefined method `stringify_keys' for #<Article:0x2544068>


I'm guessing that's because I'm trying to pass the object through the
form rather than an id reference to it?  But if I pass around just the
id, I can't really use the above hidden_field reference to set
article.parent, can I?
Mikkel B. (Guest)
on 2006-02-07 14:53
(Received via mailing list)
On Tuesday, February 07, 2006, at 1:49 PM, Duane wrote:
>
>I'm guessing that's because I'm trying to pass the object through the
>form rather than an id reference to it?  But if I pass around just the
>id, I can't really use the above hidden_field reference to set
>article.parent, can I?
>
Sure! just do find the parent using the passed parent id like:

parent = Article.find(params[:parent_id])
....stuff goes here....
new_article.parent=parent




Mikkel B.

www.strongside.dk    - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
Duane (Guest)
on 2006-02-07 16:51
Mikkel B. wrote:
>
> parent = Article.find(params[:parent_id])
> ....stuff goes here....
> new_article.parent=parent

Thank you for the reply Mikkel, but I'm not sure I understand.  Where
does this code go?  In my create method, which is receiving the results
of the form submission?  That would mean I had to pass back a parameter
called parent_id, right?  What "stuff" are you referring to?  Here's a
typical create method, via the scaffold generation:

 def create
    @article = Article.new(params[:article])
    if @article.save
      flash[:notice] = 'Article was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

I assume that I would put your code before the save command, which is
why I'm not sure what stuff might go in between those two statements.

And where did parent_id come from?  That's the hidden field I put in my
form, or is that an additional property that I add to the Article model?


Thank you!
Duane

>
>
>
>
> Mikkel B.
>
> www.strongside.dk    - Football Portal(DK)
> nflfeed.helenius.org - Football News(DK)
> ting.minline.dk      - Buy Old Stuff!(DK)
Mikkel B. (Guest)
on 2006-02-07 16:59
(Received via mailing list)
>I assume that I would put your code before the save command, which is
>why I'm not sure what stuff might go in between those two statements.
>
>And where did parent_id come from?  That's the hidden field I put in my
>form, or is that an additional property that I add to the Article model?
>
>

parent_id could/should be the name of your hidden field.

And yes, "do stuff" should occur before your save...


Mikkel B.

www.strongside.dk    - Football Portal(DK)
nflfeed.helenius.org - Football News(DK)
ting.minline.dk      - Buy Old Stuff!(DK)
This topic is locked and can not be replied to.