0bserver07
0bserver07

Reputation: 3471

Rails: is it bad practice if I do Dynamic routing through controller?

Here is what I do and I feel like it's not a good idea and it's anti-pattern:

Authenticated users get directed to Home Controller and in controller depending on user status I directed them to different routes. Is it bad if I do this:

if user.status == "accepted"
   redirect_to ### some view
else
  redirect_to ### another view
end

I thought about using CanCan, but I think it might be overkill

Upvotes: 0

Views: 105

Answers (1)

D-side
D-side

Reputation: 9485

It depends on a case. It could be acceptable. I can't give advice as that would be expressing an opinion, however, I can give you an alternative since you are clearly interested in how to do this differently.

When working with Sorcery authentication gem (and as far as I know, Devise offers the same approach) one applies a filter to a controller, that redirects any non-logged-in user to a page you specify (it's sensible to redirect to login page).

This is implemented using a before_filter (same as before_action, really). Methods fed into it are built up into a chain that is called on every controller's action (unless parameters state otherwise). If any of the filter methods redirects or renders, chain execution is halted. This can be seen in the server's logs.

This approach works really well if you need to apply that same condition and redirect to multiple actions. If it's not the case and it's the only place where you do this check, your current implementation may be just enough.

Upvotes: 1

Related Questions