miklki14567
miklki14567

Reputation: 149

Why do I keep getting a NoMethodError? I am learning rails via the "gettings started with rails" guide

I understand that this is a super noobie question and I am learning rails right now. I am following the Getting Started with Rails guide and I have followed every step correctly, but when I get to "The First Forms" section. It seems to consistently give me a "NoMethodError in Articles#new" error message. Here is the full error:

undefined method `form_with' for #<#<Class:0x007f31e8904248>:0x007f31dc290b70>
Did you mean?  form_tag

Here is the form code:

<%= form_with scope: :articles, local: true do |form| %>
  <p>
    <%= form.label :title %><br>
    <%= form.text_field :title %>
  </p>

  <p>
    <%= form.label :text %><br>
    <%= form.text_area :text %>
  </p>

  <p>
    <%= form.submit %>
  </p>
<% end %>

I can't seem to understand why it's not working. What do you think is the issue? I have seen a couple of answers which said to use form_for but that also gave me an error.

Also, I am using:

Rails: 5.0.6

Thanks ahead of time :)

Upvotes: 0

Views: 404

Answers (2)

raj_acharya
raj_acharya

Reputation: 665

If you are using rails 5.1 then form_for/form_tag is replaced with form_with

Please verify your rails version? In your case it is 5.0.6 that's why it throwing error

If it below 5.1 then use form tag or form_for

Try 1) form_for

<%= form_for @article do |form| %>
    <p> 
         <%= form.label :title %><br>
         <%= form.text_field :title %> 
     </p>
     <p>
          <%= form.label :text %><br> 
          <%= form.text_area :text %> 
      </p>
       <p> 
           <%= form.submit %> 
       </p>
 <% end %>

2) form_tag

<%= form_tag articles_path do %>
        <p> 
             <%= label_tag :title %><br>
             <%=  text_field_tag :title %> 
         </p>
         <p>
              <%= label_tag :text %><br> 
              <%= text_field_tag :text %> 
          </p>
           <p> 
               <%= submit_tag "submit" %> 
           </p>
     <% end %>

form_tag and form_for are soft deprecated and they will be replaced by form_with in the future.

If you did not have an underlying model for it, then you use form_tag.

<%= form_tag users_path do %>
   <%= text_field_tag :email %>
   <%= submit_tag %>
<% end %>

When you had a model, then you used form_for.

<%= form_for @user do |form| %>
  <%= form.text_field :email %>
  <%= form.submit %>
<% end %>

As you can see, we use form builder field helpers with form_for but we don’t do that with form_tag. So the syntax for both forms is different.

This isn’t the case with form_with, because we use form builder all the time.

form_with without a model:

<%= form_with url: users_path do |form| %>
  <%= form.text_field :email %>
  <%= form.submit %>
<% end %>

form_with with a model:

<%= form_with model: @user do |form| %>
  <%= form.text_field :email %>
  <%= form.submit %>
<% end %>

When you set the model argument then scope and url are automatically derived from it. This behaviour is similar to form_for.

Upvotes: 2

NicholasJacques
NicholasJacques

Reputation: 11

I believe form_with was introduced in Rails 5.1. You are using Rails 5.0.6. I would try changing :articles to the singular :article first though before you look into updating rails. It could be as simple as that.

Upvotes: 1

Related Questions