user290870
user290870

Reputation: 1601

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 event 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

Upvotes: 1

Views: 2476

Answers (3)

seanbehan
seanbehan

Reputation: 1601

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%>

Upvotes: 0

Jonathan Julian
Jonathan Julian

Reputation: 12264

You want to store the list in a virtual attribute. It will be available in the after_save callback.

Upvotes: 4

Voldy
Voldy

Reputation: 12868

I think the better way will be to use a tableless model. Look at Railscatsts screencast for an example. It's pretty simple.

Upvotes: 0

Related Questions