From b6c62c4596190d8f36a140fd2f9323f8ebc0c963 Mon Sep 17 00:00:00 2001 From: "Rodrigo W. Ehresmann" Date: Sat, 8 Aug 2020 13:11:14 -0300 Subject: [PATCH] Change the way polymorphic fields display links There is a problem in the way we're calling `valid_action?` in the polymorphic field. Contextualizing: The `valid_action?` implementation receives as second argument the resource class name (constantize), and so will look for the correspondent controller and check if the action exists. In the polymorphic field, we're using this to display the attribute as a link or just plain text. The problem: In the `_show` partial we check for the field attribute, that will return just the attribute name, and polymorphic fields constantly have attribute names that don't correspond whit any controller name. Here is an example: I can have an Address model that have a polymorphic association that receives the name of `owner`, and the owner can be a Shop and User model. We have the `ShopsController` and `UsersController`, but not an `OwnersController`. As result, we'll be showing the owner as plain text only. The solution: Just inform the field data class instead of the attribute name. Doing so, using the example described above, we'll be checking for the ShopsController and UsersController instead of the OwnersController, properly showing the owner as a link. --- app/views/fields/polymorphic/_show.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/fields/polymorphic/_show.html.erb b/app/views/fields/polymorphic/_show.html.erb index 42f1ff38a3..27a5645aee 100644 --- a/app/views/fields/polymorphic/_show.html.erb +++ b/app/views/fields/polymorphic/_show.html.erb @@ -17,7 +17,7 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <% if valid_action?(:show, field.attribute) %> + <% if valid_action?(:show, field.data.class) %> <%= link_to( field.display_associated_resource, [namespace, field.data],