On Feb 26, 2010, at 6:19 AM, Mark Jones wrote:
Hi,
I'll give basic rundown of what I'm trying to achieve and the problem
I have..
1) Unless a user has accepted the Site Terms and Conditions they
cannot proceed to parts of the site (tnc is part of user model)
2) The User is presented with a page just showing a page to accept the
TNC , i.e a custom page just showing the tnc field of the user model.
3) When User has accepted this I want to write the Time to the DB and
redirect to Home Page, currently this will go to the User Info page
after user update.
I have a the following in the user model:
---------------
def acceptedtnc?
if self.accepted_tnc && self.accepted_tnc_changed?
User.update(self.id, {:accepted_at => Time.now})
return true
else
return false
end
end
---------------
which is called from the User controller
---------------
before_filter :check_tncaccept
Note that before_filter will run *before* the update action fires, so
nothing will ever have _changed? set there.
The _changed? methods are tricky to work with outside of the model
callbacks - they're only set between when the values are updated and
the object's save method returns. So model callbacks see them, as do
observers - but a typical update action (using update_attributes) will
never see them.
One sneaky way around this is to set an instance variable in, say, a
before_save: (on the model)
attr_reader :just_accepted_tnc
before_save :set_accepted_at
def set_accepted_at
self.accepted_at = Time.now if accepted_tnc_changed? && accepted_tnc?
@just_accepted_tnc = true
end
---
Then you can have an update action like: (in the controller)
def update
hobo_update do
if this.just_accepted_tnc
...
end
end
end
--Matt Jones
--
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en.