开发者

RailsTutorial.org book design question

开发者 https://www.devze.com 2023-02-28 18:32 出处:网络
Hi I worked through Michael Hartl\'s RAILSTUTORIAL book and I have a question about how he builds the user\'s show page.

Hi I worked through Michael Hartl's RAILSTUTORIAL book and I have a question about how he builds the user's show page.

The page is supposed to list all the posts a user has made.

UsersController

def show
   @user = User.find(params[:id])
   @posts = @user.posts.paginate(:per_page => "10",:page => params[:page])
   @title = @user.name
 end

users/show.html.erb

<table class="profile" summary="Profile information">
  <tr>
<td class="main">
  <h1><%= @user.name %></h1>
  <%= render 'follow_form' if user_signed_in? %>
    <% unless @user.posts.empty? %>
      <table class="posts" summary="User posts">

           <%= render @posts %> # this goes to posts/_post and sends the object as post
                            # that makes the _post view use a local variable correct?
          </table>              # is there a way to do with with an @post variable?
    <%= will_paginate @posts %>
  <% end %>
 </td>
 <td class="sidebar round">
  <%= link_to avatar_for(@user), @user.avatar.url %><br />
  <strong>Name</strong> <%= @user.name %><br />
  <strong>URL</strong>  <%= link_to user_path(@user), user_path(@user) %>
  <strong>Posts</strong> <%= @user.posts.count %>
  <%= render 'shared/stats' %>
</td>   
   </tr>    
 </table>

posts/_post.html.erb

<tr>
  <td class="post">
 <span class="title"><strong><%= link_to post.title, post %></strong>&开发者_C百科lt;/span><br />

 <span class="timestamp">
  Posted <%= time_ago_in_words(post.created_at) %> ago. </span>
    <a href="<%= likers_post_path(@post) %>">Likers</a><span id="likers"><br />
</span>

  </td>

  <% if current_user?(post.user)%>
    <td>
      <%= link_to "delete", post, :method => :delete,
                                     :confirm => "You sure?",
                                     :title => post.content %>
    </td>
  <%end%>
</tr>

I need to render a partial in the users view that uses the post object, but it asks for it as @post and since no @post has been defined in the show action of the user's controller I get a nil error.

It seem odd to me to go from the user's controller to the posts view and use a local variable, which if I understand local variables correctly can't be used outsider that view. Is there a way to assign the value of post in that view to @post in the users view?

Thanks for the help


You need to use a local variable in the partial, and assign it in the locals hash. This line is a shortcut for iterating through the array and rendering the partial. I'm not sure if this works anymore in Rails 3:

<%= render @posts %>

The way I would do it:

<% @posts.each do |post| %>
  <%= render 'posts/post', :post => post %>
<% end %>

The slightly older way of rendering partials:

<% @posts.each do |post| %>
  <%= render :partial => 'posts/post', :locals => {:post => post} %>
<% end %>
0

精彩评论

暂无评论...
验证码 换一张
取 消