2009-05-03 10 views
1

Hey, je viens de commencer à apprendre rubis/rails. Pour le moment, j'essaie de faire un exemple d'un livre allemand "Praxiswissen Ruby on Rails", qui est assez vieux et écrit pour Ruby on Rails 1. De toute façon, j'ai essayé de faire les exemples avec Rails 2. Maintenant j'ai eu un problème pour plus d'une semaine.Ruby/Rails 1 & 2: problème de collection

Selon le livre (Rails 1) Je dois écrire dans mon contrôleur:

page.replace_html ( 'broadcast_search_result', : partiel => 'broadcast_search_result', : collection => @ videos ['items'] );

Je trouve que cela dans Rails 2 est écrit semblable à celle:

@items = essayer (@videos [: articles])

page.replace_html ( 'broadcast_search_results' , : partiel => @items )

Mais le serveur throws message suivant:

ActionView :: MissingTemplate (Manquant hachages de modèle/_hash.erb en application de la trajectoire de vue/views):

app/régulateurs/stations_controller.rb: 46: dans `__instance_exec0'

app/controllers /stations_controller.rb:30:in `search_broadcasts '

Je devrais créer un modèle hashes/_hash.erb au lieu de stations/_item.erb. Est-ce que quelqu'un peut m'aider s'il vous plaît?

Merci beaucoup!


OK, je vais ajouter le code du contrôleur: stations_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 

Le modèle broadcast.rb (pour Broadcast.get_videos):

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 
+0

Il serait utile de pouvoir coller le reste du code du contrôleur. Vous ne devriez pas créer un fichier _hash.erb, à moins que l'objet ne soit nommé hash.Donc, quelque chose d'autre dans votre code est de lancer cette erreur. –

+0

Merci pour la demande, j'ai ajouté le station_controller et le modèle de diffusion (De ce modèle je reçois le hash) – 27leaves

Répondre

1

Il apparaît Le problème que vous voyez est que lorsque Rails tente de rendre un partial pour un objet, il utilisera par défaut le nom de classe de cet objet comme nom pour le partial. Chaque élément individuel dans data [: items] est un Hash, donc à un moment donné, vous essayez de rendre un de ces éléments et Rails recherche un template _hash.html.erb. Je pense que vous voulez changer le code suivant:

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

à:

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

Cela devrait rendre la _item.html.erb partielle pour chaque élément @items.

+0

Merci beaucoup! Je ne sais pas pourquoi, mais c'était exactement la solution du livre ... Maintenant, la vue est juste, mais ça ne rend rien, de toute façon. La div appelée est vide. – 27leaves

+0

OK, je suis heureux maintenant. Dans mon partiel j'ai tapé thumbnail_url, mais c'est item [: thumbnail_url]. Merci beaucoup! – 27leaves

Questions connexes