我是Rails的新手。我正在使用Rails 4,但我无法找到它们如何执行此操作或调用它。在Rails 4中创建当前变量
我有这个想法从devise
在那里你可以在你的application.html.erb
文件中使用devise
和实施这样的事情:
<% if user_signed_in? %>
Logged in as <strong><%= current_user.email %></strong>.
哪里user
是色器件模型。 但是,当我尝试搜索user_signed_in
或current_user
变量时,我根本找不到它!
所以基本上我想要做的是与我创建了名为profile
另一个模型链接此user
模型(这是用于devise
)。这些模型通过它们的id链接,如果用户没有创建配置文件,则只需要求用户创建他/她的配置文件。 要做到这一点,我写这application.html.erb
:
<% if user_signed_in? && (current_profile.id != current_user.id)? %>
<%= link_to 'You have not created your profile! Please create your profile first.', update_profile_index_path, :class => 'navbar-link' %>
<% else %>
<%= yield %>
<% end %>
如预期,因为我还没有定义current_profile
不工作。我正的错误是:
undefined local variable or method `current_profile' for #<#<Class:0x000000044d6c60>:0x00000005d64110>
我的问题是,如何创建一个名为current_profile
变量,将包含当前配置文件,如current_user
就是图谋呢?
你为什么不干脆使用current_user.profile? – Rafal 2014-08-27 13:21:11
current_user是一个设计模型。它没有配置文件? – 2014-08-27 13:21:38
如果你在用户模型上定义了has_one配置文件,那么current_user.profile将起作用 – Rafal 2014-08-27 13:22:09