# How to do a new with prefilled info?

**URL:** <https://www.ruby-forum.com/t/how-to-do-a-new-with-prefilled-info/131331>\
**Category:** Rails\
**Created:** [March 8, 2008, 6:09am UTC](https://www.ruby-forum.com/t/how-to-do-a-new-with-prefilled-info/131331 "2008-03-08T06:09:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [March 8, 2008, 6:09am UTC](https://www.ruby-forum.com/t/how-to-do-a-new-with-prefilled-info/131331/1 "2008-03-08T06:09:52Z")

</div>

In my example, I have a topic that I want to have a link next to to  
create a new child.

So my erb, says something like:  
\<%= link\_to ‘New Child’, { :action =\> “newChild”, id =\> topic.id } %\>

However, the routing doesn’t like it because it thinks  
topics/newChild  
is asking for a specific topic and it can’t find one with an id of  
“newChild”

What I want is the same form as I get from “New Topic” at bottom of  
the page but with the parent already pre-filled with the id I send it.

Is there a better way to do this?

Thanks in advance,

Alan

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [March 8, 2008, 9:18pm UTC](https://www.ruby-forum.com/t/how-to-do-a-new-with-prefilled-info/131331/2 "2008-03-08T21:18:36Z")

</div>

In routes.rb:

map.resources :topics do |topic|  
topic.resources :comments  
end

With that you should be able to use this in your view:

\<%= link\_to “Add Comment”, new\_topic\_comment\_path(@topic) %\>

In your controller:

def new  
@topic = Topic.find(params[:topic\_id])  
@comment = @topic.comments.build  
end

Every request sent to the CommentsController will get the topic\_id so  
you can do nice things like listing all the comments for a particular  
topic, etc.

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [March 16, 2008, 3:31am UTC](https://www.ruby-forum.com/t/how-to-do-a-new-with-prefilled-info/131331/3 "2008-03-16T03:31:15Z")

</div>

Maybe this is answering my question, but it doesn’t seem to work.  
What I want is to create a child that is also a topic (ie, a subtopic)

I tried:  
map.resources :topices do |topic|  
topic.resources :topics  
end

and putting:  
new\_topic\_topic\_path(@topic)

That gave me a “NoMethod error”

Any suggestions?

Thanks in advance,

alan

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [March 16, 2008, 7:10am UTC](https://www.ruby-forum.com/t/how-to-do-a-new-with-prefilled-info/131331/4 "2008-03-16T07:10:22Z")

</div>

Watch the nested resources screencast on my site.

[Http://www.rubyplus.org](Http://www.rubyplus.org)  
Free Ruby & Rails screencasts

---

<div class="post-metadata">

**Author:** ![apswartz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/71e660/32.png) [@apswartz](https://www.ruby-forum.com/u/apswartz)\
**Post date:** [March 17, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/how-to-do-a-new-with-prefilled-info/131331/5 "2008-03-17T13:51:02Z")

</div>

You may want to consider introducing a new controller (Subtopics) that  
specifically deals with “topics that have parent topics”. You can  
still use the same model for state/persistence but you will greatly  
simplify your controller and view code by giving it a single focus.  
With that in mind:

map.resources :topics do |topic|  
topic.resources :subtopics  
end

class TopicsController \< ApplicationController  
def index  
@topics = Topic.find(:all, :order=\>:name, …)  
…  
end  
…  
end

class SubtopicsController \< ApplicationController  
def index  
@topic.find(:all, :order=\>:name)  
@subtopics = @topic.children  
…  
end  
end

This assumes a model that uses acts\_as\_tree or a similar data model  
for connecting ‘parent’ topics with ‘child’ subtopics.
