views:

39

answers:

1

I'm new to rails and thought I had finally figured out some of this routing stuff, but have been going in circles with this bit all day.

I was following a tutorial about building a twitter like service, and I've got the basics working from the tutorial, but with Mongo instead of mySql.

I've got 3 types of pages. The home page which is showing all the posts ordered by date

The user page which is showing the posts from a specific user

The posts page which is showing posts from a users friends.

So for each page, I've done the following

1) created a method in the corresponding controller to get the correct posts

2) created a _posts.html.erb page with the display parameters, which are slightly different on each page

3) referenced the partial in the index.html.erb page for each view.

The controller entries look like this

  def index
   @posts = Post.all(:order => 'created_at DESC')
  end

or

def posts
  @posts = Post.all(:conditions => {'user_id' => params[:id]}, :order => 'created_at DESC')
    end

and the partials are

<%= render :partial => @posts %>

In each view is a _posts.html.erb file, and each is slightly different

home/_posts.html.erb looks like this

<%= div_for post do %>
    Posted <%= time_ago_in_words(post.created_at) %> ago
    Posted By <%= post.user_id %>
    <%= post.text %>
<% end %>

while posts/_post.html.erb looks like this

<%= div_for post do %>
    Posted By <%= post.user_id %>
    <%= post.text %>
<% if post.created_at > 52.hours.since %>
    <%= distance_of_time_in_words_to_now(post.created_at) %>
        <% else %>
        <%= post.created_at.strftime("%c") %>
        <% end %>
<% end %>


Now the strange part is that on all the pages index.html.erb, users/show.html.erb, posts/index.html.erb, the partial that is being displayed is the posts/_post.html.erb. The others are being completely ignored.

my understanding was that render :partial would take the @posts and render _posts.html.erb from the current view. But this isn't happening, as only the posts/_post.html.erb is being rendered from all views.

I've looked in the routes.rb file, but don't have anything in there that would cause this problem.

Can anybody tell me why I am not displaying the proper partials?

-----------Edited --------------------------------

The directory structure for views is as follows

views 
      - home
            -_post.html.erb
            -index.htlm.erb
      - layouts
      - posts
            -_post.html.erb
            -index.html.erb
            -posts.html.erb
      - sessions 
      - users
            -_post.html.erb
            -new.html.erb
            -show.html.erb

I hope that helps.

+1  A: 

Hi Try

<%= render :partial => "post", :collection => @posts%>

maybe rails automatically defines path to the partial when you pass only collection

Bohdan Pohorilets
Thanks Bohdan, that does work, but the question is why? I shouldn't need to pass in the collection based on my understanding of RoR. Any ideas why it is needed in this instance?
pedalpete
I suppose that rails automatically defines path to the partial as "/posts/_post.rb" because when you use "form_for @person" rails parse it and the output is "form action="/persons/create" method="post"" But I may be wrong :)
Bohdan Pohorilets