语法错误,意外的 '=',预期 keyword_end flash [:notice] = "Article was submitted succsefully"

syntax error, unexpected '=', expecting keyword_end flash [:notice] = "Article was submitted succsefully"

我遇到这个错误,它不允许我查看创建的文章 这是错误 语法错误,意外的 '=',预期 keyword_end flash [:notice] = "Article was submitted succsefully"

图片

这是我的代码 article_controller.rb 文件:

class ArticlesController < ApplicationController

   def new
     @article = Article.new 
   end
   def create
  @article = Article.new(article_params)


 if  @article.save
  flash [:notice] = "Article was submitted succsefully"
  redirect_to_article_path(@article)
else
   render :new
 end 
end 
    private 
    def article_params 
       params.require(:article).permit(:title, :description)

    end





   end




end 

如果您需要任何其他文件,请向我索取

写代码要小心,往往每个字符都很重要:

flash[:notice] = "Article was submitted succsefully"

space 导致解析变得不稳定。

您的代码被解释为:

flash([:notice]) = "Article was submitted succsefully"

无效 Ruby。

另外:"succsefully" 拼写不正确。希望你还没有发货!

尝试:

respond_to do |format|
      if @article.save
        format.html { redirect_to_article_path(@article), notice: "Article was submitted succsefully" }
        format.json { head :no_content }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
 end