轨道上的红宝石 5 问题"collection"未定义的方法"to_a"你的意思是?to_s



我在将这个对象传递给分部时遇到了问题,我不知道问题是否在于这个项目最初是在Rails<5.这就是错误:

#EmptySearch的未定义方法"to_a":0x00007fd53c037d70你意思是to_s

视图:

<div class="col-12 col-lg-8 search-results-container">
<%#= render partial: 'search_bar' %>

<div class="container-fluid search-results">
<div class="row py-5" id="search-results">
<%= render partial: "components/v2/eatery_card", collection: @page.eateries %>
</div>
</div>
</div>

我在这一行遇到了问题:

<%= render partial: "components/v2/eatery_card", collection: @page.eateries %>

这是控制器:

def show
redirect_to controller: 'cities', action: 'show', location: searchkick_params[:location] if searchkick_params[:location]
@page = SearchPage.new(current_user, request, searchkick_params)
@sarray=@page.eateries
js search_results: @page.eateries, map_center: @page.map_center, ajax_results_url: api_v1_search_map_results_path, search_allergens: @page.allergen_ids, search_food_typologies: @page.food_typology_ids
end

渲染部分:

<div class="col-12 col-md-6">
<div id="eatery-card" class="eatery_result_<%= eatery_card.id %>">
<% if signed_in? && current_user.favorite_eateries.ids.include?(eatery_card.id) %>
<div class="eatery-favorite active eatery_toggle_favorite" data-id="<%= eatery_card.id %>"></div>
<% else %>
<div class="eatery-favorite eatery_toggle_favorite" data-id="<%= eatery_card.id %>"></div>
<% end %>
<% if @page %>
<%= link_to '', eatery_path(eatery_card.id, allergens: @page.allergen_ids , food_typologies: @page.food_typology_ids, searched: true), class:'eatery-link' %>
<% else %>
<%= link_to '', eatery_path(eatery_card.id), class:'eatery-link' %>
<% end %>
<div class="eatery-image" style="background-image: url(<%= eatery_card.image.url(:medium)%>);"></div>
<h5 class="eatery-name p-0 pt-3 mb-2">
<%= eatery_card.name %>
<span class="eatery-category text-success ml-1">
<%= eatery_card.category.name %>
</span>
</h5>
<h6 class="eatery-address p-0 m-0 mb-2 small"><%= eatery_card.complete_address %></h6>
<div class="eatery-rating">
<div class="er-stars mr-1 pull-left">
<%= rate_to_stars(eatery_card.reviews_score) %>
</div>
<span class="er-reviews-count text-muted small"><%= eatery_card.reviews.approved.count %> recensioni</span>
</div>
</div>
</div>

EmptySearch类:

class EmptySearch
def total_count
0
end
def took
0
end
end

我猜你从@page.eateries获得了一个EmptySearch实例,然后你将该EmptySearch传递给某个预期的Enumerable ish(如数组、ActiveRecord查询…(,然后某个调用to_a来获得一个可以迭代的数组。

快速解决方案是将to_a添加到EmptySearch:

class EmptySearch
#...
def to_a
[]
end
end

更好的解决方案是使EmptySearch可枚举。一个可能更好的解决方案是完全去掉EmptySearch,让@page.eateries为您提供某种与任何内容都不匹配的查询。

相关内容

  • 没有找到相关文章

最新更新