私はこれに間違ってアプローチしているのではないかと思います。このようなものを処理するためのRailsの方法に関するRailsコミュニティからのアドバイスを探しています:
予約リソースがあります。モデル属性は次のようになります。
# == 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
...
これらのオブジェクトには、次のようなことが起こります。
- 作成できます
- 支払いは1つに適用することができます
- 確認できます。
- それは(適切な権限を持つ誰かによって内部的に)補償することができます
- キャンセルできます
- 等...
私の問題は基本的ですが、これらのイベントをどのように処理するのですか?これらはすべてupdate
、コントローラーのアクションの一部です(最初と最後を除く)。私のコントローラーはリスになり、「におい」を始めています。今、私は「アクション」を追加し、それがコントローラーにparam
存在するかどうかを確認することで識別します。次に、それが何であるかに応じて、ユーザーがそのアクションに対して許可されていることを確認し(ただし、アクションとは、すべてparam
「アクション」であるため、更新のサブアクションを意味します)、その場合にのみ、モデルのメソッドを呼び出しますか?。 update
receive_payment(amount)
ただし、ユーザーは予約を更新(キャンセルしてparty_sizeを変更)することはできても、「支払いを受け取る」ことや「コンプ」することはできないため、これは面倒になります。私はRyanBatesのCanCangemを使用しています。承認が、それがこの素晴らしいレベルで私を助けていることを私は知りません...
たとえば、メッセージは、またはflash
のように、より具体的にする必要があります"Payment received from #{@reservation.user.name}"
"Could not comp reservation for {@reservation.user.name}"
コントローラーの一部:
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
考え?
これが間違っているように見えますか?!?
助けてくれてありがとう!