http://guides.rubyonrails.org/getting_started.html Rails错误:文章中的NoMethodError #show

时间:2014-11-05 00:30:49

标签: ruby-on-rails

我是Rails的新手,在localhost:3000 / articles / new

提交表单时看到此错误
NoMethodError in Articles#show

我正在按照http://guides.rubyonrails.org/getting_started.html教程步骤进行操作。

class ArticlesController < ApplicationController
  def 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

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

    def index
      @articles = Article.all
    end
end

这是我的代码的Github存储库的链接。我正在使用Rails版本4.1.7。任何帮助将不胜感激!

https://github.com/z20/rails_start

1 个答案:

答案 0 :(得分:2)

您的代码:

class ArticlesController < ApplicationController
  def 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

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

    def index
      @articles = Article.all
    end
end

应更改为:

class ArticlesController < ApplicationController
  def new
  end

  def create
    @article = Article.new(article_params)

    @article.save
    redirect_to @article
  end

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

  def index
    @articles = Article.all
  end     

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

show在您的代码中是私有的,应该是公开的。在您的行动之后移动私人。当您在课程中设置private时,private之后的所有内容都将无法在该特定课程之外访问。