Skip to content

Instantly share code, notes, and snippets.

@dvodvo
Last active April 2, 2023 11:30
Show Gist options
  • Select an option

  • Save dvodvo/7df4482879140ad4a1ddb0c5abf911e1 to your computer and use it in GitHub Desktop.

Select an option

Save dvodvo/7df4482879140ad4a1ddb0c5abf911e1 to your computer and use it in GitHub Desktop.
Any form, no object, just data to send, all you need is the target action route
form_with url: posts_path do |form|
Form for object, you add it **generically** to the tag. But `@post` still needs to be defined in the controller action
form_with scope: :post, url: posts_path do |form|
Form for object, safe for both post and patch paths. define `@post` in the controller action
form_with(model: @post) do |form|
Form for object, but you want to be lazy and not define `@post` in the controller action
form_with model: Post.new do |form|
Since you're using a form partial, you're possibly invoking a specific object on update:
form_with model: Post.first do |form|
or - in a more practical manner - and more to the point where `@post = Post.find(x)`
form_with model: Post.find(x) do |form|
By definition, the form is HTML over the wire.
So if you want to redirect to a view...
form_with (scope: :post, url: posts_path, local: true) do |form|
The rest is as in `form_for`
Rails shifted its stance between 5 and 6
As of Rails 6, to do UJS, you need to specify
local: false
true is on by default
Rails 7 default is turbo_stream, opt out via `, data: { turbo: false } `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment