Skip to content

Prefill new resource from params in the new action - #3077

Open
y0608 wants to merge 1 commit into
thoughtbot:mainfrom
y0608:prefill-new-action-params
Open

y0608 wants to merge 1 commit into
thoughtbot:mainfrom
y0608:prefill-new-action-params

Conversation

@y0608

@y0608 y0608 commented Nov 5, 2026

Copy link
Copy Markdown

Closes #2723

Allows the new action to build the resource from request params, so the form can be prefilled:

/admin/customers/new?customer[name]=Bob

Use cases:

  • Linking to a prefilled form, e.g. "New city" from a country page: /admin/cities/new?city[country_id]=1
  • Dynamic dropdowns: reload the form with the selected value to filter dependent selects (demo below)

Params go through resource_params, so only the dashboard's permitted attributes are used. Requests without params behave as before. The check uses param_key, so namespaced models (Blog::Post → blog_post) work too.

Demo app

Creates a Rails app using this branch, with Country / City / Person. The city select on the new person form is filtered by the selected country, which only works because new now reads params.

#!/usr/bin/env bash
set -e
rails new prefill_demo --skip-test --skip-system-test && cd prefill_demo
bundle add administrate --git https://github.com/y0608/administrate --branch prefill-new-action-params

bin/rails g model Country name
bin/rails g model City name country:references
bin/rails g model Person name country:references city:references
echo 'class Country < ApplicationRecord
  has_many :cities
end' > app/models/country.rb
echo 'class Person < ApplicationRecord
  belongs_to :country
  belongs_to :city
end' > app/models/person.rb
echo 'bg = Country.create!(name: "Bulgaria")
de = Country.create!(name: "Germany")
bg.cities.create!(name: "Sofia")
bg.cities.create!(name: "Plovdiv")
de.cities.create!(name: "Berlin")
de.cities.create!(name: "Munich")' > db/seeds.rb
bin/rails db:migrate db:seed

bin/rails g administrate:install
sed -i 's/^    city: Field::BelongsTo,/    city: Field::BelongsTo.with_options(scope: ->(field) { field.resource.country ? field.resource.country.cities : City.none }),/' app/dashboards/person_dashboard.rb
sed -i '$i\  def display_resource(r) = r.name' app/dashboards/country_dashboard.rb app/dashboards/city_dashboard.rb

# Reload the new form with the selected country so the city select gets filtered
bin/rails g administrate:views:form person
echo '<script>
  window.addEventListener("load", () => {
    $("#person_country_id").on("change", (e) => {
      window.location = "<%= new_admin_person_path %>?person[name]=" + encodeURIComponent($("#person_name").val()) + "&person[country_id]=" + e.target.value
    })
  })
</script>' >> app/views/admin/people/_form.html.erb

echo "Open http://localhost:3000/admin/people/new and change the country"
bin/rails s

Allow building the new resource from request params so forms can be
prefilled, e.g. /admin/customers/new?customer[name]=Bob. Params go
through resource_params, so only the dashboard's permitted attributes
are used. Requests without params behave as before.
@y0608

y0608 commented Nov 5, 2026

Copy link
Copy Markdown
Author

If this approach looks good, I'm happy to follow up with the same behaviour for edit.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

Creating a new resource with params

1 participant