我是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。任何帮助将不胜感激!
答案 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
之后的所有内容都将无法在该特定课程之外访问。