루비/레일 1 & 2 : 수집 문제
-
03-07-2019 - |
문제
이봐, 난 방금 루비/레일을 배우기 시작했다. 현재, 나는 독일 책 "Praxiswissen Ruby on Rails"의 예를 만들려고 노력합니다. Ruby on Rails 1을 위해 꽤 오래되고 Ruby on Rails 1을 위해 작성되었습니다. 어쨌든, 나는 Rails 2로 예제를 시도했습니다. 일주일 이상.
이 책 (레일 1)에 따르면 컨트롤러에 써야합니다.
page.replace_html ( 'broadcast_search_result', : partial => 'broadcast_search_result', : collection => @videos [ 'items']);
나는 Rails 2에서 이것을 다음과 비슷하다는 것을 알았습니다.
@items = try (@videos [: items])
page.replace_html ( 'broadcast_search_results', : partial => @items)
그러나 서버는 다음과 같은 메시지를 던집니다.
ActionView :: MissingTemplate (누락 된 템플릿 해시/_hash.erb in view path app/views) :
앱/컨트롤러/stations_controller.rb : 46 : in`__instance_exec0 '
앱/컨트롤러/stations_controller.rb : 30 : in`search_broadcasts '
스테이션/_item.erb 대신 템플릿 해시/_hash.erb를 만들어야합니다. 아무도 나를 도울 수 있습니까?
매우 감사합니다!
좋아, 컨트롤러 코드를 추가하겠습니다 : 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ü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
모델 broadcast.rb (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
해결책
당신이보고있는 문제는 Rails가 객체의 부분을 렌더링하려고 시도 할 때 해당 객체의 클래스 이름을 부분 이름으로 사용하는 것이 기본적으로 표시되는 것 같습니다. 데이터 [: 항목]의 각 개별 항목은 해시이므로 어느 시점에서 해당 항목 중 하나를 렌더링하려고 시도하고 있습니다. _hash.html.erb 템플릿을 찾고 있습니다. 다음 코드를 변경하고 싶다고 생각합니다.
page.replace_html(
'broadcast_search_results',
:partial => @items
)
에게:
page.replace_html(
'broadcast_search_results',
:partial => 'item',
:collection => @items
)
@items의 각 항목에 대해 _item.html.erb를 부분적으로 렌더링해야합니다.