Ruby on rails 如何将@resources和JSON结果组合到Rails中共享公共接口的对象数组中?

Ruby on rails 如何将@resources和JSON结果组合到Rails中共享公共接口的对象数组中?,ruby-on-rails,ruby,ruby-on-rails-3,interface,jquery-isotope,Ruby On Rails,Ruby,Ruby On Rails 3,Interface,Jquery Isotope,所以,我正在构建一个教育资源搜索引擎Rails应用程序。此应用程序将以块式布局显示结果(如微积分视频),以便于利用(用于冷过滤/排序转换等) 除了查询我们的数据库以获得适当的资源外,我们还通过谷歌的定制搜索引擎API查询谷歌。最重要的是,我们最终会希望在网格视图中放置一些广告块 所以我的问题是,如何将查询数据库返回的@resources与Google查询的JSON结果结合起来,最终与ads结合起来?我想这样做是为了使结果视图(search.html.erb)尽可能干净。此外,我希望能够对所有结果

所以,我正在构建一个教育资源搜索引擎Rails应用程序。此应用程序将以块式布局显示结果(如微积分视频),以便于利用(用于冷过滤/排序转换等)

除了查询我们的数据库以获得适当的资源外,我们还通过谷歌的定制搜索引擎API查询谷歌。最重要的是,我们最终会希望在网格视图中放置一些广告块

所以我的问题是,如何将查询数据库返回的@resources与Google查询的JSON结果结合起来,最终与ads结合起来?我想这样做是为了使结果视图(search.html.erb)尽可能干净。此外,我希望能够对所有结果进行排序/筛选。也就是说,我希望能够将ActiveRecord结果与Google查询结果合并。这也让我可以做以下事情:

(Boxx是我想到的通用类)

?


因为我可以让GoogleResult遵循与资源相同的界面。那我该怎么分类呢?嗯…

我通过阅读“在RubyonRails中设计助手”找到了我需要的东西

<% @boxxes.each do |boxx| %>
    <div class=<%= boxx.type %>>
       <h2><%= boxx.title %></h2>
       <h3><%= boxx.description %></h3>
       ...
       ...
       ...
    </div>
<% end %>
require 'will_paginate/array'

class ResourcesController < ApplicationController
  def index
      @resources = Resource.all
  end

  def create

    # Usability concern here... need to make sure that they are redirected back here once they log in or something
    if current_user == nil
      flash[:alert] = "You must log in to submit a resource!"
      redirect_to resources_path
      return
    else
      params[:resource][:user_id] = current_user.id
    end


    # Check to see if this resource unique
    params[:resource][:link] = Post::URI.clean(params[:resource][:link])
    if unique_link?(params[:resource][:link])
      @resource = Resource.new(params[:resource])
      @resource[:youtubeID] = self.isYoutube(@resource[:link])
      @resource.save
    else
      flash[:alert] = "This resource has already been added!"
    end
    redirect_to resources_path
  end

  def vote
    value = params[:type] == "up" ? 1 : -1
    @resource = Resource.find(params[:id])
    @resource.add_or_update_evaluation(:votes, value, current_user)
    respond_to do |format|  
        format.html { redirect_to :back, notice: "Thank you for voting" }  
        format.json { render :status=>200, :json=>{:success=>true}}  
    end
  end

  def isYoutube(youtube_url)
    regex = %r{http://www.youtube.com}
    if youtube_url[regex]
      youtube_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
      youtube_id = $5
      thumbnail_Link = "http://img.youtube.com/vi/#{youtube_id}/1.jpg"
    else
      thumbnail_Link = nil
    end
    thumbnail_Link
  end

  def unique_link?(url)
    Resource.find_by_link(url) == nil
  end

  def search
     @resource = Resource.full_search(params[:q])
   #  raise params.to_s
     @resource = @resource.reject!{|r| !r.media_type.eql? params[:filter][0][:media_type].downcase } if params[:filter] && !params[:filter][0][:media_type].blank?

     if params[:filter] 
      case params[:filter][0][:sort].downcase 
      when 'newest'
         then @resource = @resource.sort_by{|r| r.created_at}
      when 'votes'
         then @resource = @resource.sort_by!{|r| r.reputation_for(:votes).to_i}.reverse
      else
      end
     end

     @resource = @resource.paginate(:page => (params[:page] || 1), :per_page => 15)   
  end 


  def google(q, filter)
    # Authenticating into Google's API
    client = Google::APIClient.new(:key => 'secret', :authorization => nil)

    # Discover the Custom Search API
    search = client.discovered_api('customsearch')

    # Search Google CSE
    response = client.execute(
      :api_method => search.cse.list,
      :parameters => {
        'q' => "#{q} #{filter}",
        'key' => 'secret',
        'cx' => 'secret'
      }
    )

    # Decode the results
    results = ActiveSupport::JSON.decode(response.body, {:symbolize_names => true})

    # Return an empty array if Google CSE limit has been met.
    results["items"] == nil ? [] : results["items"]

  end

  def make_boxxes(resources, google_results, ads)

  end

end
@items = @resources | google_results