-2

im making a web app in which i have made two scaffolds user pages and page posts , but i have a problem that is how do i put the page posts index page on user pages show.html.erb and how do I filter them with the users specific for the user pages

Jesse Gallagher
  • 4,386
  • 10
  • 11
  • Your question is not clear, please elaborate. You seem to have two specific problems, please describe each of them in details. – Technoh Mar 24 '16 at 13:20

1 Answers1

1

It sounds like you want to output a list of the posts belonging to a user on a user show view. One way to do this is to extract the code used to iterate over the posts into a partial and then use that partial on both your post index and your user show view.

Partials in rails are designated by putting an underscore in front of the file name. Create a file _posts.html.erb in the posts folder and copy in the each loop from your post index into it then in your index file add this code:

<%= render partial: 'posts' %>

For it to work on your user show, in the user controller show action, assign user posts to the posts global:

@posts=@user.posts 

And add the partial to the show view:

<%= render partial: 'posts' %>

You can read more about partials on RailsGuides

Richard Erickson
  • 2,438
  • 8
  • 24
  • 36
retroGiant
  • 807
  • 7
  • 16
  • Could you please edit your question with your model definitions and the content of either the post index prior to attempting these edits or the content of the new partial? – retroGiant Mar 28 '16 at 11:29