I have seen shown code in this Go Rails-video at 05:15: Video-tutorial
<%= form_with model: Link.new do |form| %>
# ...
<% end %>
Normally I would have written a new-action. Something like:
def new
@link = Link.new
end
Obviously it's possible to skip the action and create the needed object on the fly, when invoking the form_with.
What's the benefit of having a new-action?
Is it for having a separate route, which creates the form, when invoked?
The controller method is strictly speaking not actually needed but the main advantage to it is that it places the logic in one clear coherent place.
The view is inherently messy as it's a combination of markup and code and it should really just be concerned with transforming the data it is provided with into HTML in the most straight forward way possible. Instanciating or querying for that data should be the responsibility of the controller.
This separation of concerns becomes more important as the level of complexity grows. But by actually taking the ever so slightly longer road you avoid having the re-write the code when the complexity grows.
Besides the conceptual issues there are also direct practical reasons why <%= form_with model: Link.new do |form| %>
is a not a very good practice:
While you could handle this in the view with:
<%= form_with model: @link || Link.new do |form| %>
# ...
<% end %>
This is putting the logic in the view where it doesn't belong.
Is it for having a separate route, which creates the form, when invoked?
Not really as that's possible anyways.
Even if you don't have a matching controller action rails will implicitly look for a view and render it.
It's more about separating the concerns in way that scales well to complexity and which other Rails developers expect.