I currently have this in my ApplicationController
def account_path
eval "#{current_user.type.downcase}_account_path"
end
I use it for redirects, etc. But I also want to use it in the view (for link_to's etc). Is this is a legit case to share code between the controlle开发者_如何学Gor and view to keep it DRY even though it breaks MVC?
Yes, I'd say that's a legitimate re-use. The helper_method
call is there for a reason so:
helper_method :account_path
will make this available to your views too.
If you prefer not to use eval
you could do:
def account_path
self.send("#{current_user.type.downcase}_account_path")
end
as the _path
method is interpreted as a method on the controller.
精彩评论