rails arguments to after_save observer
I want users to enter a comma-delimited list of logins on the form, to be notified by email when a new comment/post is created. I don't want to store this list in the database so I would use a form_tag_helper 'text_area_tag' instead of a form helper text_field. I have an 'after_save' observer which should send an email when the comment/post is created. As far as I am aware, the after_save ev开发者_开发问答ent only takes the model object as the argument, so how do I pass this non model backed list of logins to the observer to be passed on to the Mailer method that uses them in the cc list.
thanks
You want to store the list in a virtual attribute. It will be available in the after_save
callback.
I think the better way will be to use a tableless model. Look at Railscatsts screencast for an example. It's pretty simple.
Here are the models you would need (along w/ the form) and the virtual attribute in the user model.
# app/models/user.rb
class User < ActiveRecord::Base
# virutal attribute and validations
attr_accessor :unpersisted_info
validates_presence_of :unpersisted_info
end
# app/models/user_observer.rb
class UserObserver < ActiveRecord::Observer
def after_save(user)
# logic here...
end
end
# form for view...
<%form_for @user do |f|%>
<%= f.text_field :unpersisted_info %>
<%= f.submit "Go" %>
<%end%>
精彩评论