Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to provide scope, url, and model in form_with?

I've been following the Ruby on Rails getting started guide and when they start using partials, they only pass model into form_with and not scope or url. Thanks in advance.

new.html.erb

<%= form_with scope: :article, url: articles_path, local: true do |form| %>

edit.html.erb

<%= form_with model: @article, local: true do |form| %>

_form.html.erb

<%= form_with model: @article, local: true do |form| %>
like image 688
Evan Simanovskis Avatar asked Oct 20 '25 08:10

Evan Simanovskis


1 Answers

If you are creating/updating a model object, then you only need to pass model: @article option. Here @article is an object of Article class. In this case Rails convention-over-configuration will kick in and auto-generate the url so you don't have to pass it explicitly.

But, if you have a form, where there's no model object assoicated, e.g., a simple search form, then we can use scope option and have to pass url explicitly as in this case there's no model object associated therefore the url won't be auto-built.

In your case, it seems like you want to create or update an article object. So this should work.

<%= form_with model: @article, local: true do |form| %>

Hope that helped!

like image 170
Rajdeep Singh Avatar answered Oct 23 '25 06:10

Rajdeep Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!