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

Defining different attributes for new/create or edit/update actions without defining FORM_ATTRIBUTES makes it crash #2066

Closed
pjmartorell opened this issue Nov 23, 2021 · 6 comments
Labels
bug breakages in functionality that is implemented

Comments

@pjmartorell
Copy link

pjmartorell commented Nov 23, 2021

  • What were you trying to do?
    Upgraded from 0.13 to main (9e462f7). I tried to define different attributes for new/create or edit/update actions as stated here: https://github.com/thoughtbot/administrate/blob/main/docs/customizing_dashboards.md#form-attributes

    When trying to update or create a record in the dashboard, there is a crash. This is because, when validating permitted attributes, it's trying to get these attributes from self.class::FORM_ATTRIBUTES but it should get them from "FORM_ATTRIBUTES_#{action.upcase}" as FORM_ATTRIBUTES is not defined anymore. specific_form_attributes_for(action) is returning nil because action is not being passed in permitted_attributes. So the solution is to pass the action_name when checking permitted attributes.

  • What did you end up with (logs, or, even better, example apps are great!)?
    The affected code is related with Allow different form attributes for new/update actions #1991
    Logs:
    Screenshot_2021-11-23_at_12_43_43

    My workaround:
    In my UserDashboard added the following overrides:

    def permitted_attributes(action)
      action =
        case action
        when "update" then "edit"
        when "create" then "new"
        else action
        end
    
      form_attributes(action).map do |attr|
        attribute_types[attr].permitted_attribute(
          attr,
          resource_class: self.class.model
        )
      end.uniq
    end
    
    def form_attributes(action = nil)
      specific_form_attributes_for(action) || self.class::FORM_ATTRIBUTES
    end
    
    def specific_form_attributes_for(action)
      return unless action
    
      cname = "FORM_ATTRIBUTES_#{action.upcase}"
    
      self.class.const_get(cname) if self.class.const_defined?(cname)
    end

    In my Admin::UsersController add the following override:

    def resource_params
      params.require(resource_class.model_name.param_key)
            .permit(dashboard.permitted_attributes(action_name))
            .transform_values { |v| read_param_value(v) }
    end
  • What versions are you running?

    • Rails: 6.1.3.1
    • administrate (9e462f7)
@pjmartorell pjmartorell added the bug breakages in functionality that is implemented label Nov 23, 2021
@pablobm
Copy link
Collaborator

pablobm commented Dec 28, 2021

Thank you for the report @pjmartorell. It looks like we could have used a feature spec there. Do you think you'd be able to provide a PR?

@pjmartorell
Copy link
Author

yes, now that I'm on holidays I'll probably have time to do a PR 😅👍

@jordan-brough
Copy link
Contributor

@pjmartorell do you still plan on making a PR? I might be up for making one if not.

@nunocarvalhog
Copy link

@jordan-brough that would be great 👍 I just run into this issue too.

@pablobm
Copy link
Collaborator

pablobm commented Mar 31, 2022

@jordan-brough - Go for it! 🚀

@pablobm
Copy link
Collaborator

pablobm commented Apr 19, 2023

fixed by #2352

@pablobm pablobm closed this as completed Apr 19, 2023
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
Projects
None yet
Development

No branches or pull requests

4 participants