Rails:param丢失或值为空:文章

时间:2014-12-26 15:15:02

标签: ruby-on-rails ruby

我是Rails的新手,我开始按照rubyonrails.org教程制作一个网络应用程序。

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

这是我的红宝石代码:

class ArticlesController< ApplicationController的     def指数         @articles = Article.all     端

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分钟前一切都还不错......

感谢您的帮助

2 个答案:

答案 0 :(得分:5)

您正在尝试更新编辑方法中的文章。因此,当您导航到“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

答案 1 :(得分: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