经过一些尝试,我决定扩展feedback_controller。
在我的应用程序中,用户可以对另一个用户进行评分。我定义了以下模式:
# feedback.rb
class Feedback < ActiveRecord::Base
belongs_to :subject, class_name: 'User', foreign_key: 'subject_id'
belongs_to :writer, class_name: 'User', foreign_key: 'writer_id'
end
# user.rb
class User < ActiveRecord::Base
has_many :feedbacks, class_name: 'Feedback', foreign_key: 'subject_id'
has_many :written_feedback, class_name: 'Feedback', foreign_key: 'writer_id'
end
现在我必须定义路线,我一直在思考是否嵌套资源,如果是,如何嵌套。
这就是我试图定义路线的方式,但我不确定。通过这种方式,我只能接触到或给出给用户的反馈或用户写的反馈。
#routes
....
resources :users, except: [:new, :edit] do
resources :feedbacks, except: [:new, :edit]
end
这是构建该架构的最佳方式吗?
class FeedbacksController < ApplicationController
end
module Feedbacks
class ReceivedFeedbacksController < FeedbacksController
end
end
module Feedbacks
class GivenFeedbacksController < FeedbacksController
end
end
#routes
Rails.application.routes.draw do
resources :given_feedbaks,
only: [:index],
controller: 'feedbacks/given_feedbacks'
resources :received_feedbaks,
only: [:index],
controller: 'feedbacks/received_feedbaks'
end
resources :feedbacks, only: [:show, :create, :update, :destroy]
end