1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

mac Rails6 で 「syntax error , unexpected tIDENTIFIER, expecting keyword_end render」が出た話

Posted at

目的

  • Rails6を使用してサービスを作成中にコントローラファイルを編集しsyntax errorが出た時の解決談を書く。

エラー概要

  • render ("users/new")でsyntax errorが出ている。
  • 簡単なrenderの処理を追加しただけでエラーが出たためテンパった。
  • 時間をかけたが全く悪い箇所が見つからずにさらに焦った。
  • エラーを日本語翻訳しても該当の行に悪い部分は見当たらなかった。→時間かかってどんどん焦っていった。
  • 下記のコードをコントローラファイルに記載後、ブラウザで該当ページを確認するとsyntax errorが表示されていた。
# 問題のコード
  def create
    @user = User.new(name: params[:name], email: params[:email])
    if @user.save
      flash[:notice] = "登録完了"
      redirect_to("/users/#{@user.id}")
    else
      flash[:notice] = "登録失敗""
      render ("users/new")
    end
  end

# エラー内容(英語)
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:21: syntax error, unexpected tIDENTIFIER, expecting keyword_end render ("users/new") ^~~~~
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:21: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '(' render ("users/new") ^
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:24: unterminated string meets end of file
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:24: syntax error, unexpected end-of-input, expecting keyword_end

# エラー内容(日本語)
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:21:構文エラー、予期しないtIDENTIFIER、keyword_end render( "users / new")が必要です^ ~~~~
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:21:構文エラー、予期しないtSTRING_BEG、keyword_doまたは '{'または '(' render( "users / new")が必要です^
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:24:終了していない文字列がファイルの終わりを満たしている
/Users/admin/workspace/study/rails/eveyDayStudy/app/controllers/users_controller.rb:24:構文エラー、予期しない入力の終わり、keyword_endが必要

エラー原因

  • 問題だったのは該当の行render ("users/new")ではなく、前の行のflash[:notice] = "登録失敗""の最後のダブルクォートが余計だった。
  • まさか間違えていないであろう部分が間違えていた。
  • 下記に正しいコードを記載する。
# 問題のコード
  def create
    @user = User.new(name: params[:name], email: params[:email])
    if @user.save
      flash[:notice] = "登録完了"
      redirect_to("/users/#{@user.id}")
    else
      flash[:notice] = "登録失敗"
      render ("users/new")
    end
  end

まとめ

  • 原因はめっちゃしょうもないミスだった。
  • エラー原因箇所のみならず、前の処理がどのような書かれ方をしているかも確認す流必要があった。
  • エラー解析は焦らないこと(自分向けのメッセージ)
1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?