avo icon indicating copy to clipboard operation
avo copied to clipboard

Pre-populate fields on create pages from URL params

Open coffenbacher opened this issue 3 years ago • 2 comments
trafficstars

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.

coffenbacher avatar Aug 11 '22 02:08 coffenbacher

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] }

adrianthedev avatar Aug 11 '22 06:08 adrianthedev

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' }

coffenbacher avatar Aug 12 '22 21:08 coffenbacher

Is that on the Edit or New view? The default option works only on the new view.

adrianthedev avatar Aug 16 '22 18:08 adrianthedev

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: @.***>

coffenbacher avatar Aug 16 '22 18:08 coffenbacher

You could try and use the format_using option to coerce the default value on edit somehow. Or override it on the model layer.

adrianthedev avatar Aug 17 '22 08:08 adrianthedev