scaffoldして生成されたコードについて質問があります。
↓のcreateメソッドで@book.saveが失敗したときにformat.html { render :new }を行うことでnew.html.erbを再描画して、前に入力された値をまた表示できると解説があったのですが、どうしてそのようになるのでしょうか?
format.html { render :new }が呼ばれたら、newメソッドの@book = Book.newで@bookの中身は初期化されてしまい中身は空にはならないのでしょうか?
それともformat.html { render :new }というのはnew.html.erbだけをよんでnewメソッドは呼ばないという理解をしたら良いですか?
bookという名前でscaffoldしたbooks_controller.rbの一部
# GET /books/new def new @book = Book.new end # POST /books # POST /books.json def create @book = Book.new(book_params) respond_to do |format| if @book.save format.html { redirect_to @book, notice: 'Book was successfully created.' } format.json { render :show, status: :created, location: @book } else format.html { render :new } format.json { render json: @book.errors, status: :unprocessable_entity } end end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/28 11:42 編集
2018/05/28 11:45
2018/05/28 12:10