How to render part of code in another one


#1

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


#2

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


#3

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 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


#4

All right, thanks. I have got a users and entries controller. Users
homepage is 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.


#5

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


#6

Hi Stijn,

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

Pete


#7

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