2012-12-17 10 views
6

Ho il seguente codice nel mio controller:RoR: Modello di errore mancante durante il rendering JSON

def tljson (result = []) 
    @stat_id = params[:stat_id] 
    @rpm = FedoraRpm.find_by_name(@stat_id) 
    @rpm.ruby_gem.historical_gems.each { |h| 
    result << { :content => h.version, :start => h.build_date } 
    } 
    @rpm.bugs.each { |b| 
    result << { :content => b.name + "<br><a href='"+b.url+"'>View on BugZilla</a>", :start => b.bz_id } 
    } 
    @res = result.to_json 
    respond_to do |format| 
    format.json { render @res.to_json } 
    end 
end 

Andare /tljson.json rende il JSON, ma il titolo dice 'Template è mancante; modello mancante '. Che cosa sto facendo di sbagliato?

risposta

7

credo è necessario specificare che si desidera rendere :json nella vostra respond_to blocco

respond_to do |format| 
    format.json { render json: @res } 
end 

Altrimenti sarà cercare un file tljson.json.erb.

+0

Non è necessario chiamare '# to_json'. – Hauleth

+0

Ho provato a farlo, restituisce questo errore: SyntaxError in #tljson stats_controller.rb: 42: errore di sintassi, imprevisto ':', in attesa di '}' – 17andLearning

+0

risolto questo errore: format.json {render: json => @res} – 17andLearning

Problemi correlati