简体   繁体   English

Rails:简化控制器代码的建议

[英]Rails: suggestions for simplifying controller code

I think I might be approaching this wrong. 我想我可能正在解决这个错误。 Looking for advice from Rails community on Rails way to handle something like this: 从Rails社区寻求有关Rails方式的建议,以解决如下问题:

I have a Reservation resource. 我有一个预订资源。 Model attributes look something like: 模型属性如下所示:

# == Schema Information
#
# Table name: reservations
#
#  id              :integer          not null, primary key
   ...
#  amount_received :integer
#  party_size      :integer          default(1)
#  user_id         :integer
#  event_id        :integer
#  confirmed_at    :datetime
#  edited_by       :integer
#  comped_by       :integer
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
   ...

A number of things can happen to these objects: 这些对象可能会发生很多事情:

  • it can be created 可以创建
  • payment can be applied to one 付款可以应用于一个
  • it can be confirmed. 可以确认。
  • it can be comped (internally by someone with proper authorization) 可以进行编辑(由具有适当授权的人员在内部进行)
  • it can be cancelled 可以取消
  • etc... 等等...

My problem is basic but how to handle these events? 我的问题很基本,但是如何处理这些事件呢? they are all part of an update action (well, except 1st and last) on the controller. 它们都是控制器上update操作的一部分(除了第一个和最后一个)。 My controller is getting squirrelly and starting to 'smell'. 我的控制器越来越松动,开始发出“气味”。 Right now I identify an "action" by adding a param and then checking for the existence of that param in the controller. 现在,我通过添加一个param ,然后检查控制器中该param的存在来识别“动作”。 Then depending on what it is I check that user is authorized for said action (though by action I mean a sub-action of update because they're all update "actions") and then, only then, do I call a method on the model receive_payment(amount) . 然后根据是什么,我检查用户是否有权执行该操作(尽管操作是指更新的子操作,因为它们都是 update “操作”),然后才在该操作上调用方法型号receive_payment(amount)

This is getting cumbersome though, because a user can update their reservation (cancel and change party_size) but they can't "receive a payment" or "comp" it, etc... I'm using Ryan Bates' Can Can gem for authorization but I don't know that it's helping me at this fine level... 但是,这变得很麻烦,因为用户可以更新预订(取消并更改party_size),但是他们不能“接收付款”或“补偿”,等等...我正在使用Ryan Bates的Can Can gem作为授权,但我不知道这对我有帮助吗?

For instance, the flash messages should be more specific, like "Payment received from #{@reservation.user.name}" or "Could not comp reservation for {@reservation.user.name}" 例如, flash消息应更具体,例如"Payment received from #{@reservation.user.name}""Could not comp reservation for {@reservation.user.name}"

Piece of controller: 控制器:

  def update
    @reservation = Reservation.find(params[:id])
    # authorize! :update, @reservation #handled by load_and_authorize_resource up top
    authorize! :accept_payment, @reservation if params[:reservation][:comped_by]
    @reservation.edited_by = current_user.id if params.has_key?(:amount_received)
    ... 
    if @reservation.update_attributes params[:reservation]
      redirect_to event_reservations_path(@reservation.event_id), flash: { success: "Reservation updated" }
    else
      render action: "show", error: "Error updating reservation"
    end
  end

Thoughts? 有什么想法吗?

seems like there might be approaching this wrong?!? 似乎可能正在接近这个错误?!?

thanks for any help! 谢谢你的帮助!

You can create a method in model and use it in controller like this. 您可以在模型中创建一个方法,然后在控制器中使用它,如下所示。

def update
Reservation.method_for_all_actions(params[:some_parameter])
end

params[:some_parameter] should contain the type of action being done ie params [:some_parameter]应该包含要执行的操作类型,即

  • it can be created 可以创建

  • payment can be applied to one 付款可以应用于一个

  • it can be confirmed. 可以确认。

  • it can be comped (internally by someone with proper authorization) 可以进行编辑(由具有适当授权的人员在内部进行)

  • it can be cancelled etc... 可以取消等...

Then check in model which type of parameter[:some_parameter] is being passed and do the functionality. 然后在模型中检查要传递哪种类型的参数[:some_parameter]并执行功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM