Ruby on rails 控制器是特定于模型还是特定于操作?
假设你有以下几点Ruby on rails 控制器是特定于模型还是特定于操作?,ruby-on-rails,model,controller,Ruby On Rails,Model,Controller,假设你有以下几点 Manager model Request model User model A user makes a request. The manager then approves that request (or denies it). 所以我的问题是:approve/deny方法在Request控制器还是Manager控制器中?或者也许这并不重要 经理执行“操作”,但在后台我们正在更新模型。它可以像更改字符串一样简单 我已经用两种方法做了这件事,而且对我来说,更具体的模型似
Manager model
Request model
User model
A user makes a request. The manager then approves that request (or denies it).
所以我的问题是:approve/deny方法在Request控制器还是Manager控制器中?或者也许这并不重要
经理执行“操作”,但在后台我们正在更新模型。它可以像更改字符串一样简单
我已经用两种方法做了这件事,而且对我来说,更具体的模型似乎更自然,但我想用正确的方法做这件事,因为我还没有做过很多项目
class User < ApplicationRecord
enum role: [:employee, :manager]
has_many :applications, foreign_key: 'applicant_id'
end
class Application < ApplicationRecord
belongs_to :applicant, class_name: 'User'
has_many :approvals
end
class Approval < ApplicationRecord
belongs_to :application
belongs_to :manager, class: 'User'
validates_uniqueness_of :application_id, scope: 'manager_id'
end
以及一名控制器,以获得CRUD批准:
class ApprovalsController < ApplicationController
before_action :authenticate_user!
before_action :authorize!
before_action :set_application, only: [:new, :create, :index]
# Approve an application
# POST /applications/:application_id/approvals
def create
@approval = @application.approvals.new do |a|
a.manager = current_user
end
if @approval.save
redirect_to @application, success: 'Application Approved'
else
render :new
end
end
# Revoke approval
# DELETE /approvals/:id
def destroy
@approval = Approval.find(params[:id])
@approval.destroy
redirect_to @approval.application, success: 'Approval revoked.'
end
private
def set_application
@application = Application.find(params[:application_id])
end
# Just a minimal authorization example
# Use Pundit or CanCanCan for real world apps instead of reinventing the wheel
def authorize!
raise AuthorizationError unless current_user.manager?
end
end
class ApprovalController
实际上,我会选择第四个实体——批准。经理通过POST/requests/:request\u id/approvals
批准请求。但我认为你把问题想错了。REST是以资源为导向的,所以它以资源为导向,并对资源执行操作。您的控制器对应于资源,甚至是特定上下文中的资源。如果您被迫使用这三种模型,那么这个问题在rails世界中没有意义吗?我试图避免引入新模型,但你所说的很有道理,而且可能是不可避免的。添加一个额外的模型并不是一件坏事-让零件完成一项工作(在本例中,跟踪谁批准了什么以及什么时候批准)通常比将事情集中到大类中更可取。感谢max。快速且良好的响应!哇,谢谢你花时间把这些都写出来。今天学到了很多:D。这很有帮助。
class ApprovalsController < ApplicationController
before_action :authenticate_user!
before_action :authorize!
before_action :set_application, only: [:new, :create, :index]
# Approve an application
# POST /applications/:application_id/approvals
def create
@approval = @application.approvals.new do |a|
a.manager = current_user
end
if @approval.save
redirect_to @application, success: 'Application Approved'
else
render :new
end
end
# Revoke approval
# DELETE /approvals/:id
def destroy
@approval = Approval.find(params[:id])
@approval.destroy
redirect_to @approval.application, success: 'Approval revoked.'
end
private
def set_application
@application = Application.find(params[:application_id])
end
# Just a minimal authorization example
# Use Pundit or CanCanCan for real world apps instead of reinventing the wheel
def authorize!
raise AuthorizationError unless current_user.manager?
end
end