Ruby on rails Ruby/Rails 1&;2:收集问题

Ruby on rails Ruby/Rails 1&;2:收集问题,ruby-on-rails,collections,partials,hash,Ruby On Rails,Collections,Partials,Hash,嘿,我刚开始学习ruby/rails。目前,我试着做一本德国书“Praxiswissen Ruby on Rails”的例子,这本书非常古老,是为Ruby on Rails 1编写的。不管怎样,我试着用Rails 2来做这些例子。现在我已经有一个多星期的问题了 根据书(Rails 1),我必须在控制器中写入: page.replace\u html( “广播搜索结果”, :partial=>“广播搜索结果”, :collection=>@videos['items'] ); 我发现Rails 2

嘿,我刚开始学习ruby/rails。目前,我试着做一本德国书“Praxiswissen Ruby on Rails”的例子,这本书非常古老,是为Ruby on Rails 1编写的。不管怎样,我试着用Rails 2来做这些例子。现在我已经有一个多星期的问题了

根据书(Rails 1),我必须在控制器中写入:

page.replace\u html( “广播搜索结果”, :partial=>“广播搜索结果”, :collection=>@videos['items'] );

我发现Rails 2中的内容类似于:

@items=try(@videos[:items])

page.replace\u html( “广播搜索结果”, :partial=>@items )

但服务器会抛出以下消息:

ActionView::MissingTemplate(视图路径app/views中缺少模板哈希/_hash.erb):

app/controllers/stations\u controller.rb:46:in`\u实例\u exec0'

app/controllers/stations\u controller.rb:30:in“搜索广播”

我应该创建一个hashes/_hash.erb模板,而不是stations/_item.erb模板。有人能帮我吗

多谢各位


好的,我将添加控制器代码: 站点\u controller.rb:

class StationsController < ApplicationController
  # GET /stations
  # GET /stations.xml
  def index
    @stations = Station.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @stations }
    end
  end

  #GET /stations/search_broadcasts
  def search_broadcasts
    @search = params[:broadcast_search][:search]
    @channel = params[:broadcast_search][:channel]
    if params[:broadcast_search][:current_page]
      @current_page = params[:broadcast_search][:current_page].to_i
    else
      @current_page = 1
    end

    @videos = Broadcast.get_videos(
      @search,
      @channel,
      2,
      @current_page
    )

    render(:update) { |page|
      if @videos[:http_code] == 200
        page.replace_html(
          'broadcast_search_results_count',
          :inline => "<p>Es wurden <b><%= @videos[:count] %></b> Sendung<%= 'en' unless @videos[:count] == 1 %> gefunden</p>"
        )
      else
        page.replace_html(
          'broadcast_search_results_count',
          :inline => "<p>Es trat ein Fehler bei der Daten&uuml;bertragung auf.</p>"
        )
      end

      if @videos[:count] > 0
        logger.debug "The object is #{@videos[:items]}"
        @items = @videos[:items]
        page.replace_html(
          'broadcast_search_results',
          :partial => @items
        )
        page.replace_html(
          'broadcast_search_results_navigation',
          :partial => 'broadcast_search_results_navigation',
          :locals => {
            :videos => @videos,
            :search => @search,
            :channel => @channel,
            :current_page => @current_page
          }
        )
        page.show('broadcast_search_results')
        page.show('broadcast_search_results_navigation')
        #page.visual_effect(
        #  :highlight,
        #  'bradcast_search_results'
        #)
      else
        #page.hide('broadcast_search_results')
        #page.hide('broadcast_search_results_navigation')
      end
    }
  end

  # GET /stations/1
  # GET /stations/1.xml
  def show
    @station = Station.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @station }
    end
  end

  # GET /stations/new
  # GET /stations/new.xml
  def new
    @station = Station.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @station }
    end
  end

  # GET /stations/1/edit
  def edit
    @station = Station.find(params[:id])
  end

  # POST /stations
  # POST /stations.xml
  def create
    @station = Station.new(params[:station])

    respond_to do |format|
      if @station.save
        flash[:notice] = 'Station was successfully created.'
        format.html { redirect_to(@station) }
        format.xml  { render :xml => @station, :status => :created, :location => @station }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @station.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stations/1
  # PUT /stations/1.xml
  def update
    @station = Station.find(params[:id])

    respond_to do |format|
      if @station.update_attributes(params[:station])
        flash[:notice] = 'Station was successfully updated.'
        format.html { redirect_to(@station) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @station.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stations/1
  # DELETE /stations/1.xml
  def destroy
    @station = Station.find(params[:id])
    @station.destroy

    respond_to do |format|
      format.html { redirect_to(stations_url) }
      format.xml  { head :ok }
    end
  end
end
class StationController@stations}
结束
结束
#获取/电台/搜索广播
搜索引擎广播
@搜索=参数[:广播搜索][:搜索]
@频道=参数[:广播搜索][:频道]
如果参数[:广播搜索][:当前页面]
@当前页面=参数[:广播搜索][:当前页面]。至
其他的
@当前页面=1
结束
@视频=广播。获取视频(
@搜索,
@渠道,,
2.
@当前页面
)
呈现(:更新){|页|
如果@videos[:http_code]==200
page.replace\u html(
“广播搜索结果计数”,
:inline=>“Es wurden Sendung gefunden

” ) 其他的 page.replace\u html( “广播搜索结果计数”, :inline=>“e-trat-ein-Fehler-bei-der-Datenü;bertragung-auf.

” ) 结束 如果@videos[:count]>0 logger.debug“对象是#{@videos[:items]}” @items=@视频[:items] page.replace\u html( “广播搜索结果”, :partial=>@items ) page.replace\u html( “广播搜索结果导航”, :partial=>“广播搜索结果导航”, :本地人=>{ :videos=>@videos, :search=>@search, :channel=>@channel, :当前页面=>@当前页面 } ) page.show('broadcast\u search\u results') page.show('broadcast\u search\u results\u navigation') #视觉效果( #:突出显示, #“bradcast搜索结果” #) 其他的 #page.hide('广播搜索结果') #page.hide('broadcast\u search\u results\u navigation') 结束 } 结束 #获取/站点/1 #GET/stations/1.xml def秀 @station=station.find(参数[:id]) 回应待办事项|格式| format.html#show.html.erb format.xml{render:xml=>@station} 结束 结束 #获取/站/新 #GET/stations/new.xml def新 @station=station.new 回应待办事项|格式| format.html#new.html.erb format.xml{render:xml=>@station} 结束 结束 #获取/站/1/编辑 定义编辑 @station=station.find(参数[:id]) 结束 #邮政/车站 #POST/stations.xml def创建 @桩号=桩号。新建(参数[:桩号]) 回应待办事项|格式| 如果@station.save flash[:notice]=“站点已成功创建。” format.html{重定向到(@station)} format.xml{render:xml=>@station,:status=>:created,:location=>@station} 其他的 format.html{render:action=>“new”} format.xml{render:xml=>@station.errors,:status=>:unprocessable_entity} 结束 结束 结束 #PUT/stations/1 #PUT/stations/1.xml def更新 @station=station.find(参数[:id]) 回应待办事项|格式| 如果@station.update_属性(参数[:station]) flash[:notice]=“站点已成功更新。” format.html{重定向到(@station)} format.xml{head:ok} 其他的 format.html{render:action=>“edit”} format.xml{render:xml=>@station.errors,:status=>:unprocessable_entity} 结束 结束 结束 #删除/stations/1 #删除/stations/1.xml def销毁 @station=station.find(参数[:id]) @摧毁 回应待办事项|格式| format.html{重定向到(站点url)} format.xml{head:ok} 结束 结束 结束
模型broadcast.rb(用于broadcast.get_视频):

require('net/http'))
require('uri')
require('xmlsimple')
类广播require('net/http')
require('uri')
require('xmlsimple')

class Broadcast < ActiveRecord::Base
  belongs_to :station

  AOL_API_URL = 'http://xml.truveo.com/apiv3'
  AOL_DEVELOPER_ID = '12345667myid'

  def self.channels
    return[
      '[Alle Quellen]',
      'YouTube',
      'MYSPACE',
      'Dailymotion',
      'Google Video',
      'IFILM',
      'Veoh'
    ]
  end

  def self.get_videos(search, channel, per_page, page)

    channels = '' if channel == '[Alle Quellen]'

    url = AOL_API_URL +
      "?method=truveo.videos.getVideos" +
      "&query=" + URI.escape("#{search} type:free format:flash channel:\"#{channel}\"") +
      "&results=#{per_page.to_i}" +
      "&start=#{(page.to_i) * per_page.to_i}" +
      "&appid=#{AOL_DEVELOPER_ID}"
    data = Broadcast.get_xml(url)

    if data[:http_code] == 200
      data[:page_count] = data[:count] / per_page
      data[:page_count] += 1 if data[:count] % per_page > 0
    end

    return data
  end

  def self.get_video(video_id)
    url = AOL_API_URL +
    "?method=truveo.videos.getVideos" +
    "&query=" + URI.escape('id:' + video_id.to_s) +
    "&appid=#{AOL_DEVELOPER_ID}"
    data = Broadcast.get_xml(url)

    video = data[:items][0]
    video[:http_code] = data[:http_code]
    return video
  end

  def self.get_xml(url)

    # Data-Objekt initialisieren
    data = Hash.new
    data[:count] = 0
    data[:items] = Array.new
    data[:url] = url

    # XML-Daten holen
    xml = Net::HTTP.get_response(URI.parse(url))

    # HTTP-Response-Code ŸberprŸfen
    data[:http_code] = xml.code.to_i
    return data unless data[:http_code] == 200

    # XML Parsen
    xml_obj = XmlSimple.xml_in(xml.body.to_s)
    data[:count] = xml_obj['VideoSet'][0]['totalResultsAvailable'][0].to_i
    return data if data[:count] == 0

    # Informationen auslesen
    xml_obj['VideoSet'][0]['Video'].each { |xml_video|
      new_video = Hash.new
      new_video[:video_id] = xml_video['id'][0].to_s
      new_video[:title] = xml_video['title'][0].to_s
      new_video[:source_url] = xml_video['videoUrl'][0].to_s
      new_video[:thumbnail_url] = xml_video['thumbnailUrl'][0].to_s
      new_video[:description] = (xml_video['description'][0].to_s) if xml_video['description']
      new_video[:video_html] = (xml_video['videoPlayerEmbedTag'][0].to_s) if xml_video['videoPlayerEmbedTag']
      data[:items] << new_video
    }

    return data

 end

end
page.replace_html(
  'broadcast_search_results',
  :partial => @items
)
page.replace_html(
  'broadcast_search_results',
  :partial => 'item',
  :collection => @items
)