LoginSignup
0
0

More than 1 year has passed since last update.

render先に設定したページにインスタンス変数を渡し忘れていたこと

Posted at

render先に設定したページにインスタンス変数を渡し忘れていた

このrenderの遷移先に設定した"prototypes/show"のページにはインスタンス変数が記述してあるため、このままではインスタンス変数が空なのでエラーが出てしまいました。

def create
    @comment = Comment.new(comment_params)
    if @comment.save
      redirect_to prototype_path(@comment.prototype_id)
    else
      render "prototypes/show"  
    end
  end

そこでrenderでページを表示すると同時にインスタンス変数を渡す処理を記述することで無事にページを表示することができるようになりました。

def create
    @comment = Comment.new(comment_params)
    if @comment.save
      redirect_to prototype_path(@comment.prototype_id)
    else
      @prototype = @comment.prototype
      @comments = @prototype.comments
      render "prototypes/show"  
    end
  end

かなり初歩的なことだとは思いますが、学習時に結構詰まってしまったところだったので書かせていただきました。ページ遷移させる記述の際は、ページ先の変数の確認と、ちゃんと変数の中身を渡すことができるのかを確かめるべきだと感じました。

0
0
0

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
0
0