Devise how to access resource/resource_name in views
I am using devise in my rails application and I have two different resource types, like users and companies.
I would like to reuse some code in views, so inspite of writing
if user_logged_in?
current_user.name
else
开发者_Go百科 current_company.name
I would like to do this the way devise does:
resource.name
Is this possible?
I'm not sure exactly what you mean by "the way devise does", but you could make an application helper like apnea.diving.deep suggested. Just put this in app/helpers/application_helper.rb
(you might want to put it in a different file):
module ApplicationHelper
def resource_name
if user_logged_in?
current_user.name
else
current_company.name
end
end
end
You can now use resource_name
in your views.
(Also, just FYI, the short code snippet you wrote (if user_logged_in?...
will fail, as "if"s needs to end with "end"s)
精彩评论