简体   繁体   English

Ruby on rails 5 问题“集合”未定义方法`to_a' 你的意思是? to_s

[英]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.

相关问题 关于[:photo_one,:photo_two,:photo_three]:Array的Rails 5 Carrierwave Uploader未定义方法`to_sym&#39;:您的意思是? to_s to_yaml to_set - Rails 5 Carrierwave Uploader undefined method `to_sym' for [:photo_one, :photo_two, :photo_three]:Array Did you mean? to_s to_yaml to_set #的未定义方法`collection&#39; <Mongo::Client> 你的意思是? 集合(NoMethodError)。 红宝石 - Undefined method `collection' for #<Mongo::Client> Did you mean? collections (NoMethodError). Ruby nameerror未定义的局部变量或方法`log_out&#39;是什么意思? logout_url红宝石在轨道上 - nameerror undefined local variable or method `log_out' Did you mean? logout_url ruby on rails Ruby on Rails - # 的未定义方法`category&#39;<Post:0x000055a44cf6bd00> 你的意思是? 类别编号 - Ruby on Rails - undefined method `category' for #<Post:0x000055a44cf6bd00> Did you mean? category_id 在Rails中未定义的局部变量或方法to_a? - Undefined local variable or method to_a in Rails? Ruby to_a方法卡住 - Ruby to_a method stuck Ruby 中的 to_s 与 to_str(以及 to_i/to_a/to_h 与 to_int/to_ary/to_hash) - to_s vs. to_str (and to_i/to_a/to_h vs. to_int/to_ary/to_hash) in Ruby 为什么Ruby 1.9.2中的to_s会改变行为? - Why did to_s in Ruby 1.9.2 change behavior? Rails 错误:NoMethodError:Capistrano::Configuration:Class 的未定义方法`instance&#39; 你的意思是? 实例的? - Rails Error: NoMethodError: undefined method `instance' for Capistrano::Configuration:Class Did you mean? instance_of? Ruby on Rails模型自定义to_s方法,排除nill或空值 - Ruby on Rails model custom to_s method, exclude nill or blank values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM