pedalpete
pedalpete

Reputation: 21536

Strange routing in rails render partials

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.

Upvotes: 0

Views: 806

Answers (2)

Caleb Keene
Caleb Keene

Reputation: 388

You're passing the collection as the argument that rails is expecting to be the name of the partial. Your call to render should look like this

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

This will render app/views/posts/_post.html.erb, passing the local variable post to the partial.

Additionally, (is sometimes handy) there's an iteration object that is made available to this view, partial_name_iteration, that has information about the total size of the @posts collection, and the index of the current object.

Upvotes: 0

Bohdan
Bohdan

Reputation: 8408

"post", :collection => @posts%>

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

Upvotes: 1

Related Questions