-
Feature requestPlease elaborate on how to do dynamic link_to path MotivationI have a (Card)Component that i'd like to use for Customers and Locations. So in turn, i'd have to sometimes link_to customer_path(@card) or, respectively customer_location_path(@card.customer, @card) Would it be better to
or something else entirely? Thanks :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi there @itsyoshio! I've converted this to a discussion. We tend to use issues for bug reports and feature requests, whereas implementation support is better suited to discussions. If I'm understanding you correctly, direct :location do |model|
customer_location_path(model.customer, model)
end This allows you to call Alternatively, a method within your component that's as follows would do the trick: def card_route
@card.respond_to?(:customer) ? customer_location_path(@card.customer, @card) : customer_path(@card)
end Which option you take really depends on a few things. If you're likely to use similar code for If you want your card component to be even more flexible, maybe the route could be something you pass in when you initialize the component: def initialize(subject, route: nil)
@subject = subject
@route = route || default_card_route
end
def default_card_route
@subject.respond_to?(:customer) ? customer_location_path(@subject.customer, @card) : customer_path(@subject)
end If you wish, you could even do away with the Hope that's helpful to you! |
Beta Was this translation helpful? Give feedback.
Hi there @itsyoshio! I've converted this to a discussion. We tend to use issues for bug reports and feature requests, whereas implementation support is better suited to discussions.
If I'm understanding you correctly,
@card
refers to aCustomer
in the first instance and aLocation
in the second. Here's my suggestion as to what you could do here. Set this up in yourconfig/routes.rb
:This allows you to call
location_path(@card)
...and, more importantly,url_for(@card)
. You can now useurl_for
and pass it a customer or a location to get the desired result.Alternatively, a method within your component that's as f…