开发者

using .each in my page says undefined method

开发者 https://www.devze.com 2023-04-12 08:12 出处:网络
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:

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.


Dynamic finders like your Event.find_by_name("Party") returns an instance of your Event class. I think you need:

@upcoming_events = Event.where(:name=>"Party")


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_*

@upcoming_events = Event.find_all_by_name("Party")

Which will achieve the same result.

0

精彩评论

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

关注公众号