# How to render part of code in another one

**URL:** <https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980>\
**Category:** Rails\
**Created:** [November 24, 2008, 10:01am UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980 "2008-11-24T10:01:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [November 24, 2008, 10:01am UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980/1 "2008-11-24T10:01:33Z")

</div>

Hi,

I have a simple sideblog with my posts in it. I can see 5 last posts in  
my index action. Could I add form to submit a new post at the top of my  
posts? I dont know how to render new post action in my index action to  
show the form. THX

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [November 24, 2008, 2:20pm UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980/2 "2008-11-24T14:20:30Z")

</div>

Thx for your help. Could I do the same thing (:partial =\>) even with  
another controller? I quess not. Thx. Pete

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [November 24, 2008, 11:18am UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980/3 "2008-11-24T11:18:10Z")

</div>

You can’t render one action in another.

What you should do is put the submit form in a partial (call it  
\_form.html.erb) . See [#80 Simplify Views with Rails 2.0 - RailsCasts](http://railscasts.com/episodes/80) for more info.  
In both your new and index action you put \<%= render :partial =\> @post  
%\>. Don’t forget to instantiate @post in both your index and new  
controller - @post = Post.new . You ofcourse can also do this in the  
partial but that’s not really MVCish.

I hope this helps.  
Stijn

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [November 25, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980/4 "2008-11-25T17:47:00Z")

</div>

All right, thanks. I have got a users and entries controller. Users  
homepage is [www.mydomain.com/profile/:username](http://www.mydomain.com/profile/:username) (:controller =\> ‘users’,  
:action =\> ‘show’). Users can add a new posts in (:controller =\>  
‘entries’, :action =\> ‘new’) and I would like to allow users to add and  
see their posts at profile page, which is in users controller. Is there  
way out of this? Hope, yes. Thank you.  
Pete

Tarscher wrote:

> Do you mean call a controller via :partial =\> ? No, that’s not  
> possible.

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [November 25, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980/5 "2008-11-25T17:13:28Z")

</div>

Do you mean call a controller via :partial =\> ? No, that’s not  
possible.

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [November 26, 2008, 10:21pm UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980/6 "2008-11-26T22:21:30Z")

</div>

Hi Stijn,

thank you very much, it is exactly what I was hoping for.

Pete

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [November 26, 2008, 10:35am UTC](https://www.ruby-forum.com/t/how-to-render-part-of-code-in-another-one/153980/7 "2008-11-26T10:35:07Z")

</div>

Hi Pete,

That’s perfectly possible.

in your user show view

def show  
@entry = Entry.new  
end

in your user show view

// when using form\_for @entry rails will know that your form is  
intended for the entries controller and will point you there  
automatically.  
form\_for @entry do |f|  
// form elements here  
end

Don’t forget to redirect to the user show view in your entries create  
action.

I hope this helps.  
Stijn
