avo icon indicating copy to clipboard operation
avo copied to clipboard

has_one association is not rendering on new and edit views

Open NowakJR opened this issue 1 year ago • 12 comments

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 Screenshot 2024-01-25 at 13 46 18 1120/9adeabf6-d474-4938-a985-bb635f0492d9">

Screenshot 2024-01-25 at 13 47 41

NowakJR avatar Jan 25 '24 12:01 NowakJR

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

adrianthedev avatar Jan 25 '24 12:01 adrianthedev

Closing for now. Feel free to reopen

adrianthedev avatar Jan 25 '24 12:01 adrianthedev

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?

NowakJR avatar Jan 25 '24 14:01 NowakJR

Can you please create a minimal reproduction repository for us to have a look at and try to debug?

adrianthedev avatar Jan 26 '24 11:01 adrianthedev

would repo with created resouce, and all added views files for you to copy be sufficent?

NowakJR avatar Jan 26 '24 15:01 NowakJR

Yes @NowakJR , reproduction repositories should take minimal effort and display just what's needed in order to reproduce the issue

Paul-Bob avatar Jan 26 '24 15:01 Paul-Bob

Sure give me a minute and i will share it here in my next comment

NowakJR avatar Jan 26 '24 15:01 NowakJR

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

NowakJR avatar Jan 26 '24 16:01 NowakJR

Thank you, we'll have a look

Paul-Bob avatar Jan 26 '24 16:01 Paul-Bob

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?

Paul-Bob avatar Feb 19 '24 08:02 Paul-Bob

Oh we missunderstood each other, I will provide it as soon as i can find some time

NowakJR avatar Feb 20 '24 09:02 NowakJR

Yes, yhank you!

Paul-Bob avatar Feb 20 '24 09:02 Paul-Bob

This issue has been marked as stale because there was no activity for the past 15 days.

github-actions[bot] avatar Mar 08 '24 01:03 github-actions[bot]

Closing this because there was no activity for the past 15 days. Feel free to reopen if new information pops up ✌️

github-actions[bot] avatar Mar 23 '24 01:03 github-actions[bot]