Partially in Jbuilder does not work with associations

I am writing a batch using jbuilder to return json data. I have a module in which I return data to the record in the show method and all the records in the index. But all the records do not work, that's what I did my show method in the controller

def index
@signals = Signal.all.paginate(:page => params[:page], :per_page => 10)

end

def show
 @signal = Signal.find(params[:id])
end

Here is my partial _signal.json_jbuilder

json.(@signal, :id, :power)
json.user do
 json.first_name @signal.user.first_name
json.last_name @signal.user.last_name
 json.email @signal.user.email
end
json.location @signal.signal_location, :latitude, :longitude
json.device do
json.model @signal.device.phonemodel
json.os_version @signal.device.os_version
json.os_type @signal.device.os_type
json.sim_card_provider @signal.device.sim_card_provider
end

now i use this partial in my show.json.jbuilder and index.json.jbuilder here is my show.json.jbuilder

json.partial! 'signals/signal', signal: :@signal 

it works fine, but when i use this partial in index.json.jbuilder here is my index.json.jbuilder

json.partial! partial: 'signals/signal', collection: @signals, as: :signal

he says undefined @ signal.user.first_name. It reaches the partial part, but cannot reach associations. I checked the logs ... but nothing really useful ... stuck with it since then ... help rate

+4
1

, .

json.(signal, :id, :power)
json.user do
  json.first_name signal.user.first_name
  json.last_name signal.user.last_name
  json.email signal.user.email
end
json.location signal.signal_location, :latitude, :longitude

show.json.jbuilder

json.partial! 'signals/signal', signal: @signal

index.json.jbuilder

json.partial! partial: 'signals/signal', collection: @signals, as: :signal
+10

Source: https://habr.com/ru/post/1524649/


All Articles