2017-01-22 156 views
0

我对Ruby on Rails非常新颖。我试图通过制作简单的博客来了解指南并学习。当试图访问本地主机是给我在标题中的错误。我确信这是一个简单的解决方法,目前我无法看到它。谢谢!articles_controller.rb:26:语法错误,意外的输入结束,期待keyword_end

class ArticlesController < ApplicationController 
    def new 
    @article = Article.new 
    end 
    def create 
    @article = Article.new(article.params) 

    if @article.save 
     redirect_to @article 
    else 
     render 'new' 

    end 

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

    def index 
    @articles = Article.all 
    end 

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

如果在创建方法中缺少结尾 – Doon

回答

2

create行动加入end字。这必须努力

class ArticlesController < ApplicationController 
    def new 
    @article = Article.new 
    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 

    def index 
    @articles = Article.all 
    end 

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

并附加“结束”我得到一个新错误,“未定义方法”每个为零:NilClass“ –

+0

'article_params'而不是'article.params '我猜 – VAD

+0

谢谢你VAD!这有帮助。对我来说是漫长的一天,讨厌犯错误,哈哈。 –

4

压痕

如果你的文本编辑器不能自动缩进代码,用一个又一个!

如果你的文本编辑器可以缩进代码,请使用它;)

class ArticlesController < ApplicationController 
    def new 
    @article = Article.new 
    end 
    def create 
    @article = Article.new(article.params) 

    if @article.save 
     redirect_to @article 
    else 
     render 'new' 

    end 

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

    def index 
     @articles = Article.all 
    end 

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

你可以看到,def create用正确的缩进的最后一个方法定义,所以这个问题必须从这里来。

PARAMS

您定义article_params方法,但调用article.params。这可能是另一个问题。

私有方法

private关键字是私人被定义的任何方法。不只是article_params,而且showindex,在你的情况。我猜最后两个应该是公开的(即关键字private以上)。

+0

关于私人修复后的方法的评论。我看到你在那里说什么,现在这很有道理。非常感谢! –

+0

如果这解决了您的问题,请将您的问题标记为已回答。 – toughskin

相关问题