vendredi 26 janvier 2018

NoMethodError in Articles#new in form_for using Rails 4.2.5 tutorial

I am trying to complete the official Rails tutorial and on step 5.2. The instructions are to do a form_with, but i got an error, looked up this StackOverflow post: Rails form_with results in a NoMethodError. I changed the form_with to form_for like the answer said, and i get this error:

NoMethodError in Articles#new

Showing /home/ubuntu/workspace/app/views/articles/new.html.erb where line #2 raised:

undefined method 'model_name' for {:scope=>:article, :url=>"/articles", :local=>true}:Hash

Extracted source (around line #2):

1. <h1>New Article</h1>
2. <%= form_for scope: :article, url: articles_path, local: true do |form| %>
3.   <p>
4.     <%= form.label :title %><br>
5.     <%= form.text_field :title %>
6.   </p>

Rails.root: /home/ubuntu/workspace

app/views/articles/new.html.erb:2:in '_app_views_articles_new_html_erb__1707235537542743350_40377020'

I am using Cloud9, if it helps. Here is my code for the view and controller:
View:

<h1>New Article</h1>
<%= form_for scope: :article, url: articles_path, 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 %>

Controller:

class ArticlesController < ApplicationController
  def new
  end

  def create
    render plain: params[:article].inspect
  end
end

Aucun commentaire:

Enregistrer un commentaire