开发者

Devise how to access resource/resource_name in views

开发者 https://www.devze.com 2023-02-02 07:27 出处:网络
I am using devise in my rails application and I have two different resource types, like users and companies.

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消