0

I have a User model that can post microblogs and it shows up on the user show page but I was wondering how would I be able to show the user made microblogs onto a model that the user belongs to for instance in this case a school. Users belong to specific schools and the schools have many users under them. All help much appreciated!

User Show Page

<div id="MicropostBody">
 <div>
 <% if @user.microposts.any? %>
    <table class="microposts">
      <%= render @microposts %>
    </table>
    <%= will_paginate @microposts %>
 <% end %>
 </div>
</div>

School Show Page Same thing?

<div id="MicropostBody">
 <div>
 <% if @user.microposts.any? %>
    <table class="microposts">
      <%= render @microposts %>
    </table>
    <%= will_paginate @microposts %>
 <% end %>
 </div>
</div>

User Controller

def show
  @user = User.find(params[:id])
  @school = School.find(params[:id])
  @micropost = Micropost.new
  @microposts = @user.microposts.paginate(page: params[:page])
end

School Controller Same thing??

def show
  @user = User.find(params[:id])
  @school = School.find(params[:id])
  @micropost = Micropost.new
  @microposts = @user.microposts.paginate(page: params[:page])
end

New School Controller

def show
  @school = School.find(params[:id])
  @user = User.new
  @micropost = Micropost.new
  @microposts = @school.microposts.paginate(page: params[:page])
  @micropost = current_school.microposts.build
end
Kellogs
  • 471
  • 1
  • 4
  • 17
  • 2
    You need to take a look at [partials](http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials). – Bradley Priest Feb 16 '12 at 06:31
  • @Bradley Thanks Bradley, you been helping me a lot with my questions lately haha, mmm do you think you can lend me a little more hand on this? – Kellogs Feb 16 '12 at 07:22

1 Answers1

1

Take a look back at the partials section of the Rails tutorial book before reading any further, if you're still struggling.

users/_microposts.html.erb

<div id="MicropostBody">
  <div>
    <% if microposts.any? %>
      <table class="microposts">
        <%= render microposts %>
      </table>
      <%= will_paginate microposts %>
    <% end %>
  </div>
</div>

Then in both views you can use:

<%= render 'users/microposts', :microposts => @microposts %>

Bradley Priest
  • 7,438
  • 1
  • 29
  • 33
  • The rendering partials helped thanks Bradley but this is what really changed it, I updated the code! – Kellogs Feb 18 '12 at 03:31