Yield a two separate .rb files in one view
I'm new to rails. I would like to yield my "_posts" form next to my database results. How can I do this?
I am currently yielding the index.html.erb by default twice using this but I want the second to be the _form.html.erb file so that I can write a post 开发者_运维技巧while watching the database:
<tr>
<td><%= yield %></td>
<td><%= yield %></td>
</tr>
Eventually I hope to refresh both the form and the list of posts with ajax. but that's for later.
<tr>
<td><%= yield %></td>
<td><%= render :partial => "form" %></td>
</tr>
What you need is not yield - which is usually used in layout - but render template.
It looks like your _posts form is already in a partial and so you can include that partial in the template for your index page:
In index.html.erb include:
<%= render( :partial => "<model-name>/<partial_name>" %>
Where <model_name>
is that of the model your posts form references and <partial_name>
is the name of the partial (i.e. the filename of where you have the form.
e.g.
<%= render( :partial => "posts/new" %>
A partial is simply denoted by adding an underscore to the start of the filename. i.e. if before it was new.html.erb
then make it _new.html.erb
.
There is lots of good information on partials and rendering in the Rails docs: http://guides.rubyonrails.org/layouts_and_rendering.html
精彩评论