Rails:缺少参数或值为空:article

问题描述 投票:3回答:2

我是Rails的新手,我根据rubyonrails.org教程开始制作一个Web应用程序。

我的应用程序是一个带有文章的博客。.我实现了创建和编辑功能,这些功能运行得很好,但是在尝试访问http://localhost:3000/articles/2/edit以编辑文章时突然出现错误。错误是ActionController::ParameterMissing in ArticlesController#edit param is missing or the value is empty: articles

这是我的红宝石代码:

类ArticlesController

def new
    @article = Article.new
end

def edit
    @article = Article.find(params[:id])
    if @article.update(article_params)
        redirect_to @article
    else
        render 'edit'
    end
end

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

def create
    @article = Article.new(article_params)
    if @article.save
        redirect_to @article
    else
        render 'new'
    end
end

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

错误警报所针对的行是params.require(:articles).permit(:title, :text)我真的不知道错误可能在哪里,因为2分钟前一切都还好...

谢谢您的帮助

ruby-on-rails ruby
2个回答
6
投票

您正在尝试使用edit方法更新文章。因此,当您导航到“ articles / 2 / edit /”时,它将尝试更新文章2。但是您没有传递任何参数。

我认为您可能想要的是:

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

def update
  @article = Article.find(params[:id])
  if @article.update(article_params)
    redirect_to @article
  else
    render 'edit'
  end
end

0
投票

我知道已经晚了,但是我希望此解决方案可以帮助某人。在ArticleController中需要添加以下行:

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

和这个...

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

    if @article.update(article_params)
      redirect_to @article
    else
      render 'edit'
    end
end
© www.soinside.com 2019 - 2024. All rights reserved.