Railstutorial.org 书籍,更改为嵌套路由

发布于 2024-11-03 10:04:18 字数 2115 浏览 1 评论 0原文

大家好,我一直在关注 Rails 教程书,创建用户和帖子以及显示帖子的提要。然而,作者从未使用过嵌套资源,这在Rails中似乎非常重要,我想自己探索如何使用它们。然而,当我根据 Ruby on Rails 指南嵌套帖子资源时,它随后破坏了我的所有表单和路径。

我不想重新开始,而是想切换到嵌套资源,并在此过程中准确了解差异是什么。谁能帮我解决这个问题?感谢您的任何帮助。

我特别困惑的是如何处理 feed。当前 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

这是从本章第 11.3.3 节开始的 http:// ruby.railstutorial.org/chapters/user-microposts#top 并在本章 12.3 中为实数构建 http://ruby.railstutorial.org/chapters/following-users#top

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生池水覆流年 2024-11-10 10:04:18

以下是一些可以帮助您入门的链接:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文