I am trying to assign a message to flash[:notice] in a model observer.
我试图在模型观察者中为flash [:notice]分配一条消息。
This question has already been asked: Ruby on Rails: Observers and flash[:notice] messages?
这个问题已经被问到:Ruby on Rails:Observers和flash [:notice]消息?
However, I get the following error message when I try to access it in my model:
但是,当我尝试在我的模型中访问它时,我收到以下错误消息:
undefined local variable or method `flash' for #<ModelObserver:0x2c1742c>
Here is my code:
这是我的代码:
class ModelObserver < ActiveRecord::Observer
observe A, B, C
def after_save(model)
puts "Model saved"
flash[:notice] = "Model saved"
end
end
I know the method is being called because "Model saved" is printed to the terminal.
我知道正在调用该方法,因为“模型保存”被打印到终端。
Is it possible to access the flash inside an observer, and if so, how?
是否有可能在观察者中访问闪存,如果是这样,如何?
2 个解决方案
#1
11
I needed to set flash[:notice]
in the model to override the generic "@model was successfuly updated".
我需要在模型中设置flash [:notice]来覆盖泛型“@model was successfuly updated”。
This what I did
这就是我做的
- Created a virtual attribute in the respective model called
flash_notice
- 在相应的模型中创建了一个名为flash_notice的虚拟属性
- Then I set the virtual attribute in the respective model when needed
- 然后我在需要时在相应的模型中设置虚拟属性
- Used an after_filter when this virtual attribute was not blank to override the default flash
- 当此虚拟属性不为空时使用after_filter来覆盖默认闪存
You can see my controller and model how I accomplished this below:
你可以看到我的控制器和模型,我在下面完成了这个:
class Reservation < ActiveRecord::Base
belongs_to :retailer
belongs_to :sharedorder
accepts_nested_attributes_for :sharedorder
accepts_nested_attributes_for :retailer
attr_accessor :validation_code, :flash_notice
validate :first_reservation, :if => :new_record_and_unvalidated
def new_record_and_unvalidated
if !self.new_record? && !self.retailer.validated?
true
else
false
end
end
def first_reservation
if self.validation_code != "test" || self.validation_code.blank?
errors.add_to_base("Validation code was incorrect")
else
self.retailer.update_attribute(:validated, true)
self.flash_notice = "Your validation as successful and you will not need to do that again"
end
end
end
class ReservationsController < ApplicationController
before_filter :authenticate_retailer!
after_filter :flash_notice, :except => :index
def flash_notice
if !@reservation.flash_notice.blank?
flash[:notice] = @reservation.flash_notice
end
end
end
#2
18
No, you set it in the controller where the saving is occurring. flash
is a method defined on ActionController::Base
.
不,您将其设置在正在进行保存的控制器中。 flash是ActionController :: Base上定义的方法。
#1
11
I needed to set flash[:notice]
in the model to override the generic "@model was successfuly updated".
我需要在模型中设置flash [:notice]来覆盖泛型“@model was successfuly updated”。
This what I did
这就是我做的
- Created a virtual attribute in the respective model called
flash_notice
- 在相应的模型中创建了一个名为flash_notice的虚拟属性
- Then I set the virtual attribute in the respective model when needed
- 然后我在需要时在相应的模型中设置虚拟属性
- Used an after_filter when this virtual attribute was not blank to override the default flash
- 当此虚拟属性不为空时使用after_filter来覆盖默认闪存
You can see my controller and model how I accomplished this below:
你可以看到我的控制器和模型,我在下面完成了这个:
class Reservation < ActiveRecord::Base
belongs_to :retailer
belongs_to :sharedorder
accepts_nested_attributes_for :sharedorder
accepts_nested_attributes_for :retailer
attr_accessor :validation_code, :flash_notice
validate :first_reservation, :if => :new_record_and_unvalidated
def new_record_and_unvalidated
if !self.new_record? && !self.retailer.validated?
true
else
false
end
end
def first_reservation
if self.validation_code != "test" || self.validation_code.blank?
errors.add_to_base("Validation code was incorrect")
else
self.retailer.update_attribute(:validated, true)
self.flash_notice = "Your validation as successful and you will not need to do that again"
end
end
end
class ReservationsController < ApplicationController
before_filter :authenticate_retailer!
after_filter :flash_notice, :except => :index
def flash_notice
if !@reservation.flash_notice.blank?
flash[:notice] = @reservation.flash_notice
end
end
end
#2
18
No, you set it in the controller where the saving is occurring. flash
is a method defined on ActionController::Base
.
不,您将其设置在正在进行保存的控制器中。 flash是ActionController :: Base上定义的方法。