anyone got a slicker way to do this?
http://drawohara.tumblr.com/post/4517961
thanks.
-a
You could have login! return false if it renders and then do something like:
def post_comment return unless login!
or
def post_comment login! || return
Not very pretty and kind of a little too implicit versus the throw/catch approach but it's less complex than the throw/catch for the case you show in your post.
marcel
Ara,
Is there a reason why you couldn’t put this into a before filter and avoid the problem altogether.
This is the way it’s done in the restful_authentication plugin
before_filter :login_required, :(except | only) => [actions]
This way the action never gets called if the login_required method returns false. Then the login_required method can call a render or redirect etc.
Sorry If I’m missing something…
Cheers
Daniel
anyone got a slicker way to do this?
You could have login! return false if it renders and then do something like:
def post_comment return unless login!
or
def post_comment login! || return
right. i'm way too stupid the remember that every time though
Not very pretty and kind of a little too implicit versus the throw/catch approach but it's less complex than the throw/catch for the case you show in your post.
completely agree. the issue, i guess, is that there is no way to pull out any logic that does a render under some condition and place it outside the controller. so every controller then has to duplication the code. of course there are filters, but that's not well suited to something you only do some of the time.
in this case though your solution or a filter is far easier.
kind regards.
-a
that certainly seems to be the obvious answer here - i should have picked a better example for that post!
so yeah, that’s fine here - i was more generally wondering about being able to factor out and arbitrary peice of logic that might cause something to be rendered and the login seemed like the obvious example.
cheers.
-a