Railstutorial.org book, changing to nested routes

和自甴很熟 提交于 2019-12-12 01:33:46

问题


Hi I've been following along in the Rails Tutorial book, creating users and posts and a feed to show the posts. However, the author never used nested resources, which seem to be very important in rails and I would like to discover how to use them myself. However, when I nest the post resource according to Ruby on rails guides, it subsequently breaks all my forms and paths.

Instead of starting over I would like change over to nested resources and in the process learn exactly what the differences are. Can anyone help with how I would go about this? Thanks for any help.

Particularly I'm puzzled by how to do with the feed. Currently feed_item calls the old post_path.

shared/_feed_item partial

<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>

micropost controller

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

This is started in section 11.3.3 in this chapter http://ruby.railstutorial.org/chapters/user-microposts#top and built for reals in 12.3 of this chapter http://ruby.railstutorial.org/chapters/following-users#top


回答1:


Here are some links that will get you started:

railscasts.com/episodes/139-nested-resources
railscasts.com/episodes/196-nested-model-form-part-1
railscasts.com/episodes/197-nested-model-form-part-2



来源:https://stackoverflow.com/questions/5800399/railstutorial-org-book-changing-to-nested-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!