搜索API和显示数据而不首先保存到数据库 - Ruby on Rails

时间:2014-07-08 13:40:02

标签: ruby-on-rails ruby json ruby-on-rails-3 model-view-controller

我已经在一个项目上工作了几个星期,并且在显示我从各种api获得的数据时遇到了问题。我想使用<%= render @ search.products%>所以我可以对结果进行分页,但我不断收到以下错误。

ArgumentError in Searches#show

Showing /app/views/searches/show.html.erb where line #10 raised:

'{:total_count=>69, :incomplete_results=>false, :items=>[{.... json data here .....}]}' is   not an ActiveModel-compatible object that returns a valid partial path.
Extracted source (around line #10):

10:     <%= render  @search.products %>

模型search.rb

def candidates  
  @search ||= find_product.items
end 

def store
.... Store Client ....
end 

private

  def find_products
    products = store.search_products('location:"#{location}"', order: "desc") if location.present?
    products = store.search_products("#{size} ", order: "desc") if size.present?
    products = store.search_products("#{brand}", order: "desc") if brand.present?
    products
  end

Controller searching_controller.rb

  def show
    @search = Search.find(params[:id])
    respond_to do |format|
      format.html # show.html.erb
      format.json {render json: @search.candidates }
    end
 end

1 个答案:

答案 0 :(得分:2)

而不是这样做:

<%= render @search.products %>

如果您有部分 app / views / products / _product.html.erb ,请尝试以下操作:

<%= render partial: 'products/product', collection: @search.products %>

有一个很好的blogpost可以解释如果你想要更多信息,你的错误来自哪里。