2014-09-19 8 views
5

Ho cercato di aggiungere attributi personalizzati a jbuilder come faccio nella pagina di presentazione alla mia pagina di indice per l'impaginazione con paginazione e la mancata visualizzazione degli attributi personalizzati.Come aggiungere attributi extra alla pagina dell'indice di jbuilder

per esempio quello che ho nella mia azione di controllo è

def index 
    #respond_with 
    @publishers = Publisher.paginate(:page => params[:page], :per_page => 30) 
    respond_to do |format| 
     format.json 
    end 
    end 

e il mio index.json.jbuilder è

json.array!(@publishers) do |publisher| 
    json.extract! publisher, :id, :name, :url 
    json.categories do 
    publisher.categories.each do |category| 
     json.name category.name 
     json.id category.id 
     json.url url_for(category) 
    end 
    end 
end 

quello che vorrei avere è

json.current_page @publishers.current_page 
json.total_pages @publishers.totla_entries 

json.array!(@publishers) do |publisher| 
    json.extract! publisher, :id, :name, :url 
    json.categories do 
    publisher.categories.each do |category| 
     json.name category.name 
     json.id category.id 
     json.url url_for(category) 
    end 
    end 
end 

così ho la pagina corrente e le pagine totali mostrate nell'output json della pagina indice.

attualmente Quello che ho è

[{"id":1,"name":"facebook","url":"http://www.facebook.com","categories":{"name":"Art and Crafts","id":1,"url":"/categories/1-art-and-crafts"}}] 

come posso ottenere questo risultato. sto anche usando willpaginate

risposta

7

Dopo il lungo trambusto, e dando un'occhiata a come funziona il template dello show di jbuilder ho capito che il json.array! metodo è stato override nulla al di fuori del blocco così ho fatto qualche tweeks e risolto dal rap in un nodo radice come qui di seguito

json.current_page @publishers.current_page 
json.total_pages @publishers.total_entries 
json.total_records Publisher.count 

json.publishers do |publishersElement| 
    publishersElement.array!(@publishers) do |publisher| 
    json.extract! publisher, :id, :name, :url 
    json.categories do 
     publisher.categories.each do |category| 
     json.name category.name 
     json.id category.id 
     json.url url_for(category) 
     end 
    end 
    end 
end 

e l'uscita ho ottenuto è stato questo

{"current_page":1,"total_pages":1,"total_records":1,"publishers":[{"id":1,"name":"Bellanaija","url":"http://www.bellanaija.com","categories":{"name":"Art and Crafts","id":1,"url":"/categories/1-art-and-crafts"}}]} 
+2

Grazie per pubblicare la tua risposta questo mi ha aiutato Penso che due dei tuoi metodi siano sbagliati. Dovresti utilizzare 'json.total_pages @ publishers.total_pages' e' json.total_records @ publishers.total_records'. – flyingL123

+0

'@ publishers.total_records' va bene così anche' total_entries' funzionava come al 2014 – Uchenna