avo
avo copied to clipboard
Pre-populate fields on create pages from URL params
Feature
I would love it if there was a way to pre-populate fields on the create page from the URL as a flexible way to link from custom tools to the standard [C]RUD page. For example:
http://localhost:3000/avo/resources/widgets/new?name=hello%20world
Current workarounds
I imagine this might be possible using custom fields and Javascript but I haven't gotten that far yet. I thought about creating a record and then linking to the edit page, but there were some required fields I couldn't robustly set to defaults.
Hey man.
Have you tried the default option? It also takes a block where you can use the params global object.
field :name, as: :text, default: -> { Avo::App.params[:name] }
Oh great, that callback looks like exactly what I need.
Only issue now is that the default doesn't seem to work on text fields for me. The select field example in the default documentation works for me.
I.e. these both have seem to have no effect on the field content as far as I can tell:
field :name, as: :text, default: 'John'
field :name, as: :text, default: -> { 'John' }
Is that on the Edit or New view? The default option works only on the new view.
That's on the /new view indeed.
Makes sense why the edit view wouldn't work 👍
On Tue, Aug 16, 2022, 11:33 AM Adrian Marin @.***> wrote:
Is that on the Edit or New view? The default option works only on the new view.
— Reply to this email directly, view it on GitHub https://github.com/avo-hq/avo/issues/1136#issuecomment-1217003450, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAB4A24IOOA6NYFNRPR2EKTVZPNITANCNFSM56GS4C2Q . You are receiving this because you authored the thread.Message ID: @.***>
You could try and use the format_using option to coerce the default value on edit somehow. Or override it on the model layer.