在我的页面中使用 .each 表示未定义的方法
我正在尝试获取名称为“Party”的事件列表。我还有所有事件的列表,效果很好。所以我把它放在我的pages_controller中:
@events = Event.all.paginate(:page => params[:page])
@upcoming_events = Event.find_by_name("Party")
这就是我的主页的样子:
<% if !signed_in? %>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
<ul class="users">
<% @upcoming_events.each do |event| %>
<%= render :partial => 'events/eventhome', :locals => {:event => event} %>
<% end %>
</ul>
<% else %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Events", events_path %></li>
<li><%= link_to "Profile", current_user %></li>
<h3>Upcoming Events</h3>
<ul class="users">
<% render @events %>
</ul>
<% end %>
出于某种原因,当我执行find_by_name时,它会弄乱主页中的.each。我做错了什么?它说.each未定义的方法。
I am trying to get a list of the events with name "Party". I also have a list of all of the events, which works fine. So I put this in my pages_controller:
@events = Event.all.paginate(:page => params[:page])
@upcoming_events = Event.find_by_name("Party")
and here's what my homepage looks like:
<% if !signed_in? %>
<%= link_to "Sign up now!", signup_path, :class => "signup_button round" %>
<ul class="users">
<% @upcoming_events.each do |event| %>
<%= render :partial => 'events/eventhome', :locals => {:event => event} %>
<% end %>
</ul>
<% else %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Events", events_path %></li>
<li><%= link_to "Profile", current_user %></li>
<h3>Upcoming Events</h3>
<ul class="users">
<% render @events %>
</ul>
<% end %>
for some reason when I do find_by_name it messes up the .each in the homepage. What am I doing wrong? It says .each undefined method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
动态查找器,例如您的
Event.find_by_name("Party") 返回 Event 类的实例。我认为你需要:
Dynamic finders like your
Event.find_by_name("Party")
returns an instance of your Event class. I think you need:lucapette的答案包含正确的原因这。
但是,您可以使用动态查找器来查找这些,您可以使用 find_all_by_*
这将达到相同的结果。
lucapette's answer as contains the correct reason for this.
You can however use a dynamic finder to find these, you can use find_all_by_*
Which will achieve the same result.