Search code examples
ruby-on-railsjsonjbuilder

rails helper to populate json feed


I would like to use a helper method to dictate which links are available to my json feed. I had this set up using standard html like this

  ..........
  if shopping_request.new?
    content += accept_shopping_request(shopping_request)
    content += reject_shopping_request(shopping_request)
  end
  content += view_send_outs_from_me(shopping_request) if shopping_request.send_out_in_process?
  content += view_send_backs_to_me(shopping_request) if shopping_request.send_back_in_process?
  ..........

I am trying to implement the same behaviour but this time using the helper in a jbuilder file. I've tried various configurations but can't get the links to display. I am using a basic example to test

  def available_routes_as_json(item)
    Jbuilder.new do |json|
      json.contact contact: "mailto:#{item.delivery_contact_email}"
    end
  end

And then the relevant bit of the jbuilder file

  json.buttons do
    available_routes_as_json(item)
  end

Thanks


Solution

  • The answer was fairly straightforward really.

    Because Jbuilder isn't strictly part of the MVC pattern, I had to include helpers into my controller.

     include ActionController::Helpers
    

    this pretty much sums up what's going on.