こんにちは、私は 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 で開始され、この章の 12.3 で実際にビルドされますhttp://ruby.railstutorial.org/chapters/following -ユーザー#トップ