Railstutorial.org 书,更改为嵌套路由 [英] Railstutorial.org book, changing to nested routes

查看:44
本文介绍了Railstutorial.org 书,更改为嵌套路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注 Rails 教程,创建用户和帖子以及显示帖子的提要.但是作者从来没有使用过嵌套资源,这在rails中似乎很重要,我想自己探索如何使用它们.但是,当我根据 Ruby on rails 指南嵌套 post 资源时,它随后破坏了我的所有表单和路径.

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.

尤其让我对如何处理提要感到困惑.当前 feed_item 调用旧的 post_path.

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

<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

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

推荐答案

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

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

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

这篇关于Railstutorial.org 书,更改为嵌套路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆