Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasOne with a HasOne attribute shows object.to_s #1142

Closed
hcarreras opened this issue Apr 13, 2018 · 3 comments
Closed

HasOne with a HasOne attribute shows object.to_s #1142

hcarreras opened this issue Apr 13, 2018 · 3 comments
Labels
bug breakages in functionality that is implemented fields new fields, displaying and editing data

Comments

@hcarreras
Copy link

Hi there,

On the show view of a HasOne association, the attributes of the association are being shown:

Because some of the attributes are HasOne associations as well, the object it's just converted to string.

Locally, I've fixed this by checking if the attribute has an associated resource:

app/views/fields/has_one/_show.html.erb

<% if field.data %>
  <fieldset class="attribute--nested">
    <legend>
      <%= link_to(
              field.display_associated_resource,
              [namespace, field.data],
          ) %>
    </legend>
    <% field.nested_form.attributes.each do |attribute| -%>
      <div>
        <dt class="attribute-label">
          <%= t(
                  "helpers.label.#{resource_name}.#{attribute.name}",
                  default: attribute.name.titleize,
              ) %>
        </dt>
        <dd class="attribute-data attribute-data--<%= attribute.html_class %>">
          <% if attribute.methods.include? :display_associated_resource  %>
            <%= link_to(
                    attribute.display_associated_resource,
                    [namespace, attribute.data],
                ) %>
          <% else %>
            <%= attribute.data %>
          <% end %>
        </dd>
      </div>
    <% end -%>
  </fieldset>
<% end %>

I think the result looks nicer since it allows a simple navigation between models:

Would this be something you are interested to have? Should I make a PR?

@nickcharlton
Copy link
Member

Yes! This would be a great addition.

Are you expecting that the values which are displayed would be something that is/should be configurable?

@hcarreras
Copy link
Author

I don't think we should make this configurable.
The view of a has_one association simply displays the values of the association. In the case of another has_one association this should be its resource name.
The link to the association it's just an added feature to improve the navigation.

@nickcharlton nickcharlton added bug breakages in functionality that is implemented fields new fields, displaying and editing data labels Jan 2, 2020
@nickcharlton
Copy link
Member

I'm going to close this because I think #1259 is the more ideal solution to this (showing some of the nested forms attributes) and it's been quite a while. Thanks for opening!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug breakages in functionality that is implemented fields new fields, displaying and editing data
Projects
None yet
Development

No branches or pull requests

2 participants