[英]Ruby on rails 5 problem "collection" undefined method `to_a' Did you mean? to_s
I'm having trouble passing this object to the partial, I can't figure out if the problem is that this project was initially developed on a version of Rails <5 This is the error:我无法将此对象传递给部分对象,我无法确定问题是否是该项目最初是在 Rails <5 版本上开发的,这是错误:
undefined method `to_a' for #EmptySearch:0x00007fd53c037d70 Did you mean?
#EmptySearch:0x00007fd53c037d70 的未定义方法`to_a' 你的意思是? to_s
to_s
view:看法:
<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>
the problem occurs to me in this line:我在这一行中出现了问题:
<%= render partial: "components/v2/eatery_card", collection: @page.eateries %>
this is the controller:这是控制器:
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
render partial:渲染部分:
<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 类:
class EmptySearch
def total_count
0
end
def took
0
end
end
I'd guess that you're getting an EmptySearch
instance from @page.eateries
and then you're passing that EmptySearch
to something is expected something Enumerable-ish (such as an array, ActiveRecord query, ...), then that something calls to_a
to get an array that it can iterate over.我猜你从
@page.eateries
得到了一个EmptySearch
实例,然后你把EmptySearch
传递给了一些预期的 Enumerable-ish(例如数组,ActiveRecord 查询,...),然后那个东西调用to_a
来获取一个可以迭代的数组。
A quick fix is to add to_a
to EmptySearch
:快速解决方法是将
to_a
添加到EmptySearch
:
class EmptySearch
#...
def to_a
[]
end
end
A better solution would be to make EmptySearch
Enumerable.更好的解决方案是使
EmptySearch
可枚举。 A possibly even better solution would be to get rid of EmptySearch
entirely and have @page.eateries
give you a query of some sort that doesn't match anything.一个可能更好的解决方案是完全摆脱
EmptySearch
并让@page.eateries
为您提供某种不匹配任何内容的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.