嗨,我一直在关注 Rails 教程一书中,创建用户和帖子以及显示帖子的提要。但是,作者从未使用过嵌套资源,这在 Rails 中似乎非常重要,我想自己探索如何使用它们。但是,当我根据 Ruby on rails 指南嵌套发布资源时,它随后会破坏我的所有表单和路径。
我不想重新开始,而是想切换到嵌套资源,并在此过程中准确了解差异是什么。任何人都可以帮助我解决这个问题吗?谢谢你的帮助。
特别是我对如何处理提要感到困惑。目前 feed_item 调用旧的 post_path。
shared/_feed_item 部分
<tr>
<td class="avatar">
<%= link_to avatar_for(feed_item.user), feed_item.user %>
</td>
<td class="post">
<span class="title"><%= link_to feed_item.title, feed_item %></span><br />
<span class="content">the plot: <%= feed_item.content %></span><br />
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
</td>
</td>
<% if current_user?(feed_item.user) %>
<td>
<%= link_to "delete", feed_item, :method => :delete,
:confirm => "You sure?",
:title => feed_item.content %>
</td>
<% end %>
</tr>
微柱控制器
class Micropost < ActiveRecord::Base
.
.
.
default_scope :order => 'microposts.created_at DESC'
# Return microposts from the users being followed by the given user.
scope :from_users_followed_by, lambda { |user| followed_by(user) }
private
# Return an SQL condition for users followed by the given user.
# We include the user's own id as well.
def self.followed_by(user)
followed_ids = %(SELECT followed_id FROM relationships
WHERE follower_id = :user_id)
where("user_id IN (#{followed_ids}) OR user_id = :user_id",
{ :user_id => user })
end
end
这从本章http://ruby.railstutorial.org/chapters/user-microposts#top的第 11.3.3 节开始,并在本章http://ruby.railstutorial.org/chapters/following的第 12.3 节中构建-用户#顶部