Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A administrate
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 96
    • Issues 96
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 32
    • Merge requests 32
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • thoughtbot, inc.
  • administrate
  • Issues
  • #2066
Closed
Open
Issue created Nov 23, 2021 by Administrator@rootContributor

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

Created by: pjmartorell

  • What were you trying to do? Upgraded from 0.13 to main (9e462f71). 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 https://github.com/thoughtbot/administrate/pull/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 (9e462f71)
Assignee
Assign to
Time tracking