Rails form_with导致NoMethodError [英] Rails form_with results in a NoMethodError

查看:95
本文介绍了Rails form_with导致NoMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从头开始重新构建博客,但遇到了有关创建新文章的形式的问题.

I'm re-building a blog from the ground up and a ran into a problem concerning the form for creating new articles.

<%= form_with scope: :article, url: articles_path, local: true do |form| %>导致错误undefined method `form_with'.

Rails提供以下建议: Did you mean? form_tag

Rails provides the following suggestion: Did you mean? form_tag

尝试使用form_tag时,出现以下错误:undefined method `label' for nil:NilClass

Upon trying form_tag, I get the following error: undefined method `label' for nil:NilClass

使用form_with

<h1>New Blog Post</h1>
<%= form_with 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 %>

articles_controller.rb

articles_controller.rb

class ArticlesController < ApplicationController
    def index
       @articles = Article.all 
    end

    def show
       @article = Article.find(params[:id]) 
    end

    def new
        @article = Article.new
    end

    def create
        @article = Article.new(article_params)

        @article.save
        redirect_to @article
    end

    private
        def article_params
            params.require(:article).permit(:title, :text)
        end
end

当我将form_with更改为form_tag时,出现以下错误:undefined method `label' for nil:NilClass

When I change form_with to form_tag I get the following error: undefined method `label' for nil:NilClass

为了记录,我正在使用

  1. Ruby 2.3.4
  2. 导轨4.2.5

推荐答案

您要使用form_for @article do |form|( 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆