【问题标题】:NoMethodError in Static_pages#homeStatic_pages#home 中的 NoMethodError
【发布时间】:2012-06-14 12:42:25
【问题描述】:

我的 Static_pages#home 中出现无方法错误:

未定义的方法“存在吗?”对于注册表

 The little code I'm using it in is:

 <% if user_signed_in? && current_user.registry.exists? %>

 <%= link_to "Show My Registry", current_user.registry %>

 <% else %>

 <%= link_to "Create a new registry", new_registry_path %>

 <% end %>

我应该在家用控制器中添加一些东西吗?

提前致谢。

【问题讨论】:

  • 不是控制器,不——它在抱怨 registry 对象。

标签: ruby-on-rails database nomethoderror


【解决方案1】:

exists? 方法是一个类方法。您必须这样做:

Registry.exists?  # Is there any registery?

或指定注册表的 id:

Registry.exist?(current_user.registry_id)

这里有一些参考:

http://apidock.com/rails/ActiveRecord/Base/exists%3F/class

祝你好运!

【讨论】:

  • 谢谢 - 我使用它并得到了下面的答案!
【解决方案2】:

最终成功了:

<% if user_signed_in? && Registry.exists?(current_user.registry) %>

【讨论】:

  • 太棒了!如果我的回答有帮助,请接受(请随时编辑并在最后添加您的 cmets)。
猜你喜欢
  • 1970-01-01
  • 2011-04-25
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 2015-07-08
相关资源
最近更新 更多