avo
avo copied to clipboard
has_one association is not rendering on new and edit views
Context
I have polymorphic relation Student has_one :user, as: :registerable. User table contains email, first_name and last_name. Is there a way to render those fields on new and edit views so I can create User alongside Student in one dashboard? Currently my Student resource looks like this:
# frozen_string_literal: true
module Avo
module Resources
class Student < Avo::BaseResource
self.includes = []
self.title = :email
self.model_class = ::Student
def filters
filter Avo::Filters::StudentStatus
end
def actions
action Avo::Actions::ExportStudentEmailsCsv
action Avo::Actions::UpdateStudentsActiveUntil
action Avo::Actions::BanAllStudents
end
self.search = {
query: -> {
query.ransack(
id_eq: params[:q],
m: "or",
user_email_cont: params[:q],
m: "or",
user_first_name_cont: params[:q],
).result(distinct: true)
}
}
def fields
field :id, as: :id, hide_on: [:show, :new]
field :email, as: :text
field :first_name, as: :text
field :last_name, as: :text
field :phone_number, as: :text
field :date_of_birth, as: :date
field :invitation_token_sent_at, as: :date_time, hide_on: [:index]
field :active_until, as: :date, sortable: true
field :active_since, as: :date
field :status, as: :select, enum: ::Student.statuses, sortable: true
field :registration_completed, as: :boolean
field :lessons, as: :has_many
field :ratings, as: :has_many
field :company, as: :belongs_to
field :user, as: :has_one, show_on: [:edit]
end
end
end
end
And it renders email, first_name and last_name only on index and show page.
System configuration
Avo version: 3.2
Rails version: 7.0.8 Ruby version: 3.2.3 License type
- [x] Community
- [ ] Pro
Are you using Avo monkey patches, overriding views or view components?
- [ ] Yes. If so, please post code samples.
- [x] No
Screenshots or screen recordings
<img width="1103" alt="Screenshot 2024-01-25 at 13 46 03" src="https://github.com/avo-hq/avo/assets/8020
1120/9adeabf6-d474-4938-a985-bb635f0492d9">
There isn't a feature like that yet (nested resource creation). There is a manual recipe that you could apply to your codebase.
You may show the has many on the edit view if you need so using this setting.
We'd take a PR for this feature if we had one.
Thanks @NowakJR
Closing for now. Feel free to reopen
Thanks so much for the solution! The view itself works but i have a problem with permitted params. Once I have put all user attributes into extra_params, student attributes suddenly are not permitted?
Can you please create a minimal reproduction repository for us to have a look at and try to debug?
would repo with created resouce, and all added views files for you to copy be sufficent?
Yes @NowakJR , reproduction repositories should take minimal effort and display just what's needed in order to reproduce the issue
Sure give me a minute and i will share it here in my next comment
https://github.com/NowakJR/avo-reproduction-repo there it is, these are all files added for that feature, The view itself works fine but there is just a problem with permited params and User instance is not saved. @adrianthedev @Paul-Bob
Thank you, we'll have a look
would repo with created resouce, and all added views files for you to copy be sufficent?
@NowakJR I misunderstood your question. When we say minimal reproduction repository we mean an runnable application only with the models / resources that are needed to reproduce the bug.
I'm not able to reproduce the bug from the provided files, there are missing the models, migrations etc... Can you please generate a new rails application with avo installed and with the models / resources / tools that are necessary for us to reproduce it?
Oh we missunderstood each other, I will provide it as soon as i can find some time
Yes, yhank you!
This issue has been marked as stale because there was no activity for the past 15 days.
Closing this because there was no activity for the past 15 days. Feel free to reopen if new information pops up ✌️