Search code examples
ruby-on-railsruby-on-rails-5jbuilder

How can you use polymorphic_path inside a jbuilder view with custom prefix?


Trying to generate the path for a polymorphic association. I've tried, but keep getting errors.

json.(payment, :created_at, :type, :check_number, :amount_cents, :payable_type, :payable_id)

# json.payable_path Rails.application.routes.url_helpers.polymorphic_path(:custom_name, payment.payable)

Must I create a payable_path method on the payment model?


Solution

  • Here's how I do it for a comment in my app. On the comment model commentable is a polymorphic association.

    Here are a couple lines from my _comment.json.jbuilder

    json.extract! comment, :owner, :body, :created_at, :updated_at
    json.commentable_path polymorphic_url(comment.commentable)