I'm using RABL in a Rails app to access data via REST. It's working except for the if statement.
I get this error:
undefined local variable or method `matitem_id'
This is my show.json.rabl code:
object @expense
attributes :id, :unitcost, :quantity, :markup, :exp_date, :created_at, :description, :pcard, :invoice
child :employee do
attributes :id, :maxname
end
child :vendor do
attributes :id, :vendor_name
end
if matitem_id != nil
child :matitem do |matitem|
attributes :id, :itemnum
end
end
UPDATE1
I also tried
if @expense.matitem_id != nil
If matitem_id
is an attribute of the @expense
object you should reference it in a conditional through the root_object
helper like this:
if root_object.matitem_id
child :matitem do |matitem|
attributes :id, :itemnum
end
end
Also, the != nil
is probably redundant.
This line:
if :matitem_id != nil
Is literally comparing the symbol :matitem_id
to nil
. This will NEVER be true. What you need to do is compare the ID of the child object. You can pass the object into the block:
child :matitem do |matitem|
if matitem.id != nil
...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With