开发者

Show a Notification in Rails after updating a Model

开发者 https://www.devze.com 2023-03-15 06:50 出处:网络
Is it possible to use the ActiveRecord Callbacks before_create after_create before_save after_save before_destroy

Is it possible to use the ActiveRecord Callbacks

  • before_create
  • after_create
  • before_save
  • after_save
  • before_destroy
  • after_destroy

to display notifications using jquery to the user?

Can someone provide 开发者_C百科an example??

Thanks


It's not the responsibility of the model to comunicate back to the view. In my opinion this should be handled in the controller, not the model. The controller could look like:

respond_to :json
def create
  if @page.save
    respond_with {:message => "Aye Okay!"}
  else
    respond_with {:message => "Oops, something went wrong."}
  end
end

The destroy and before create cases could follow the same idea: check manually in the controller and send the message from there.

Not exactly what you asked but I think you should keep them separated.

0

精彩评论

暂无评论...
验证码 换一张
取 消