How to show index and instance items on same page?
I have a bunch of Posts.
- I want that开发者_Go百科 for the index action, the list of posts should be displayed in a sidebar and some random text in the right side.
- For a action to display a specific post, the list of posts should still be displayed in a sidebar and the post itself on the right side. (Please see attached image for more details)
What is the idiom to do this in Rails? What's the "right" way?
UPDATE
@coreyward, thanks:
So, I would have:- _sidebar_list.html.erb (for list of posts)
- index.html.erb => render sidebar and random text
- show.html.erb => render sidebar and post
But then, the layout (left side bar, right content) (the black box in image) would need to be repeated in index and show templates. Is the solution to this making yet another partial (containing left side bar, right content) and then rendering that partial in index and show?
I'm assuming you know how to do the HTML, and that you already know your "random content". That said, the index
action should be pretty straightforward: grab the collection of posts and render links to them in the sidebar there. It would make sense to put these in a partial like "_sidebar_list.html.erb" and rendering it from there like so:
<%= render :partial => 'sidebar_list', :collection => @posts %>
For your show
action, you would want to grab the same collection of posts for display in the sidebar in your controller…such as:
@post = Post.find(params[:id])
@posts = Post.all
Then you would render the sidebar list in your show view, in addition to displaying the data for the current post.
<nav>
<!-- render line from above -->
</nav>
<article>
<h1><%= @post.title</h1>
...
</article>
Hope that helps you get started in the right direction. Feel free to comment if you're unclear on anything.
精彩评论