Ruby on rails “错误”;缺少参数或值为空:article";
我想创建一个“article”的实例,浏览器给了我一个错误 缺少参数或值为空:article 这是我的文章控制器Ruby on rails “错误”;缺少参数或值为空:article";,ruby-on-rails,Ruby On Rails,我想创建一个“article”的实例,浏览器给了我一个错误 缺少参数或值为空:article 这是我的文章控制器 class ArticlesController < ApplicationController def index @article = Article.all end def new @article = Article.new end def create @article
class ArticlesController < ApplicationController
def index
@article = Article.all
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
end
def show
@article = Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title, :body)
end
end
class-ArticlesController
如何修复?这是引发错误的方法
def article_params
params.require(:article).permit(:title, :body)
end
这意味着您没有将此类参数传递给控制器。确保在“新建/编辑”视图中有一个表单,并且正确地传递了它的值。此错误表示新表单中缺少
article
参数或该参数为空。进入项目/views/new.html.erb
并确保您实际上正在将:article
传递到url:articles\u path
<%= form_for :*article*, url: articles_path do |f| %>
我在文章中输入了一个错误(不小心输入了文章),产生了完全相同的错误。试试这段代码,它对我有用:
def article_params
params.require(:articles).permit(:title, :body)
end
我理解这种错误方法。但我认为这都是真的,我做了一个记录,它保存在数据库中并显示在页面上,我决定创建另一个记录和一个错误。С