如何编写这些资源的路由?

How do I write the routes for these resources?

对于我的 rails 应用程序,关联如下:

我的 routes.rb 文件:

Rails.application.routes.draw do

  root 'welcome#index'
  get 'home', :to => 'home#index'
  get 'searchApis', :to => 'home#searchApis'

  devise_for :users, :controllers => { registrations: 'registrations' }

  resources :users, shallow: true do
    resources :bookmarks, except: :new
    resources :friendships, only: [:index, :show, :destroy]
    resources :reminders
  end

  resources :bookmarks, shallow: true do
    resources :comments
  end

end

我写的这些路由正确吗?

当我 rake routes 时,我得到 bookmarks#index 两次,所以我很困惑。其中一个的前缀是bookmark,另一个是bookmarks。为什么会这样?

根据我的理解,应用程序不需要查看索引中的所有书签,因为它们只对创建它们的用户可见。但是,我希望用户的朋友可以看到提醒。

如果可能的话,我希望得到清理路线的建议。我真的怀疑我这样做是否正确。

Shallow 仅提供:index、:new 和:create。所以你得到了两次索引。一次来自用户和其他书签 - 评论。

关于 re-reading 你在 post 开头的关联,以及评论 belongs_to 用户和书签,创建多态关系可能是个好主意。

粗略的指南适用于您的模型,

class Comment < ActiveRecord::Base
  belongs_to :messages, polymorphic: true
end

class User < ActiveRecord::Base
  has_many :comments, as: :messages
end

class Bookmark < ActiveRecord::Base
  has_many :comments, as: :messages

然后rails generate migration Comments,如果你还没有,让它看起来像下面这样:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :name
      t.references :messages, polymorphic: true, index: true
      t.timestamps null: false
    end
  end
end

否则 运行 迁移以将列添加到您的 Comment 模型。即 rails g migration AddMessagesToComments messages:references 但是一定要打开上面命名的新迁移文件,并在 rake db:migrate

之前添加 polymorphic: true

我对你的规范的解读:

#config/routes.rb
resources :users, only: [] do #-> show a user's collections (no edit)
   resources :bookmarks, shallow: true, except: [:new, :edit, :update]                 #-> url.com/bookmarks/:id
   resources :comments, :friendships, :reminders, shallow: true, only: [:index, :show] #-> url.com/comments/:id
end

resource :bookmarks, except: :index do #-> url.com/bookmarks/:id
   resources :comments #-> url.com/bookmarks/:bookmark_id/comments/:id -- should be scoped around current_user
end

对于评论控制器,执行此操作:

#app/controllers/comments_controller.rb
class CommentsController < ApplicationController
   def new
      @bookmark = Bookmark.find params[:bookmark_id]
      @comment  = @bookmark.comments.new
   end

   def create
      @bookmark     = Bookmark.find params[:bookmark_id]
      @comment      = @bookmark.comments.new bookmark_params
      @comment.user = current_user
      @comment.save
   end
end

不要制作 welcomehome 控制器,您不需要它们。

您可以在 application 控制器中放置 off-hand 个动作:

#config/routes.rb
root                   'application#index'
get 'home',        to: 'application#home'
get 'search_apis', to: 'application#search_apis'

当然这有点像 antipattern(你最终会膨胀你的 ApplicationController),但如果你在其他控制器中只有模糊的 "one-off" 动作,你'将最适合使用上述内容。


此外,仅使用 snake_case with lowercase for URL's - HTTP's spec 确定所有 URL 应作为小写处理:

Converting the scheme and host to lower case. The scheme and host components of the URL are case-insensitive. Most normalizers will convert them to lowercase. Example: → http://www.example.com/

虽然这仅适用于 domain/host,但它也适用于 URL 本身。