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

belongs_to with class_name not working #323

Closed
migu0 opened this issue Dec 11, 2015 · 8 comments
Closed

belongs_to with class_name not working #323

migu0 opened this issue Dec 11, 2015 · 8 comments

Comments

@migu0
Copy link

migu0 commented Dec 11, 2015

I have the following column and relationship, which works fine in AR:

# schema.rb
create_table "organizations", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
   t.uuid     "headquarters_id"
end

# organization.rb
belongs_to :headquarters, class_name: 'Location', foreign_key: "headquarters_id"

# organization_dashboard.rb
ATTRIBUTE_TYPES = {
  headquarters: Field::BelongsTo
}

When editing an Organization record, I get the attached error. How can I fix this? Thx

screen shot 2015-12-12 at 10 41 19 am

@c-lliope
Copy link
Contributor

Hey, @michaelimstepf! In organization_dashboard.rb, try changing:

  headquarters: Field::BelongsTo

to:

  headquarters: Field::BelongsTo.with_options(class_name: "Location")

Let me know if that solves your problem.

@migu0
Copy link
Author

migu0 commented Dec 13, 2015

@Graysonwright Awesome, thanks, that worked.

Another question that I couldn't find an answer for in the documentation.
Is there any way to pass in allow_blank: true to a belongs_to field so users can make no selection when saving the record?
And for a has_many (through) field, is there a way to pass in the already saved associated records to the multiple select field? Let's say I have an Organization that has many Locations. When I edit the Organization, the Locations field is always empty and when I save the record, it clears out all associated Locations unless I set them manually each time I edit the Organization.

@c-lliope
Copy link
Contributor

@michaelimstepf what version of Administrate are you using? I think we may have fixed the HasMany bug you mentioned in #247 (discussion in #244).

As for the allow_blank: true question, that might have been addressed in #312. If you're using Administrate ~> 0.1.2, that should be the default behavior.

@migu0
Copy link
Author

migu0 commented Dec 13, 2015

@Graysonwright You're a legend, upgrading from 0.1.1 to 0.1.2 fixed both issues.

Is there any way I can pass in an HTML class to a field? I saw in the Ruby docs that there's an html_class method but it doesn't accept any argument. I'm basically trying to add some custom styling (I have a verified checkbox/boolean for some attributes that I want to float left to the field it applies to).

@c-lliope
Copy link
Contributor

@michaelimstepf The html_class method is part of the private API - it's not used for customizing the generated HTML.

The best way to do that right now would be to generate the view and override the HTML.

I think we could add better support for this by accepting a html_class option, like:

name: Field::String.with_options(html_class: "username")

If we take that approach, the question comes up of how this is handled on the different pages. Would we apply the html class to the form as well as the table cell, or would we allow users to pass in different classes for each of the pages?

Another option would be to add another HTML class by default to all of the fields, with the format .attribute-foobar. That would let users select any field they want with selectors like .cell-data.attribute-name.

What are your thoughts on those options?

@migu0
Copy link
Author

migu0 commented Dec 15, 2015

@Graysonwright Thx. They syntax you posted looks very good to me.

Given that all three pages (index, show, edit) use different html attributes, I would just apply the same class to all of the three pages and then it's up to the user to limit the scope to a specific html element.

The other option you suggested (HTML class by default to all of the fields) would work but I think is inferior for the following reasons:

It's less DRY, in your CSS you'll maintain a list of attributes for a specific style as opposed to just using one single class:

.some-class {}
.attribute-name, .attribute-gender, .attribute-nationality {}

And also, even though it's a class, it behaves more like an id, which is a bit confusing.

Then lastly, for me it would be very useful to have the class being applied to the wrapping element:

The reason for this request is that for my use case I actually want to have certain related form fields floating next to each other. It's always easy to target a child within a class for those who want to only style an input or label tag but with pure CSS there's no sane way to target a parent element if the CSS is being applied to the input field for instance.

@c-lliope
Copy link
Contributor

We've gotten a bit off-topic, and I think it would be good to move this into its own issue. I'm going to close this and add another issue for it.

@c-lliope
Copy link
Contributor

Moved to #351.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants