質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2010閲覧

rails transaction で rescue 以降の処理が実行されない

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/01/03 09:19

編集2021/01/03 09:45

rails のcreateメソッドでtransactionを使っていて、エラーに引っかかっています。

modelに presence: true のバリデーションを設定したので、

properties#createのtransactionの処理が上手くいかなかった場合に、
rescue以降の処理をしたいです

しかし、rescue後の処理は実行されません。

transactionの処理が失敗した場合の rescue 以降の処理を実行させたい時、どのように修正すれば良いですか?

app/model/property.rb

class Property < ApplicationRecord has_one :property_detail, dependent: :destroy accepts_nested_attributes_for :property_detail validates :name, presence: true, length: { maximum: 30 } end

app/model/property_details.rb

class PropertyDetail < ApplicationRecord belongs_to :property, dependent: :destroy validates :address, presence: true, uniqueness: true validates :postal_code, presence: true, length: { is: 6 } validates :built_at, presence: true validate :check_built_before_today def check_built_before_today return if built_at.blank? errors.add(:built_at, "は今日以前のものを選択してください") if self.built_at > Date.today end end

app/controllers/properties_controller.rb

def new @property = Property.new @property_details = PropertyDetail.new end def create begin ActiveRecord::Base.transaction do @property = Property.new(property_params) @property.save! details = create_property_details_params[:property_detail] details[:property_id] = @property.id @property_details = PropertyDetail.new(details) @property_details.save! end flash[:success] = "New property registared!" redirect_to properties_path rescue ActiveRecord::RecordInvalid => e flash.now[:danger] = "failed" render 'new' rescue ActiveRecord::RecordNotSaved => e flash.now[:danger] = "failed" render 'new' end end

app/views/properties/new.html.erb

<% provide(:button_text, 'Add new property') %> <%= render 'layouts/side' %> <div class="main"> <h1>Add new property</h1> <%= form_with model: @property do |f| %> <%= f.label :name %> <%= f.text_field :name, class: 'form-control' %> <%= f.fields_for @property_details do |i| %> <%= i.label :address %> <%= i.text_field :address, class: 'form-control' %> <%= i.label :postal_code %> <%= i.number_field :postal_code, class: 'form-control' %> <%= i.label :built_at %> <%= i.date_field :built_at, class: 'form-control' %> <% end %> <%= f.submit yield(:button_text), class: "btn btn-primary" %> <% end %> </div>

rescue後の処理は実行されず、
logを見ると
ROLLBACKされずにサーバーエラー(500)

development.log

1ActionView::Template::Error: 2 undefined method `model_name' for nil:NilClass

と表示されています。
properties#newでモデルのインスタンスは指定しているので、なぜこのようなエラーが表示されるのかも疑問です。

足りない情報があれば指摘していただけると幸いです。よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

createでも定義が必要です。newを実行した時の controllerインスタンスはもうなくなっていますので。
@property.save! で例外が発生して rescueに飛んでますので
@property_details = PropertyDetail.new(details)
が実行されていないので、@property_details が未定義でエラーになっています。
rescueの中でで定義してください

投稿2021/01/03 12:15

winterboum

総合スコア23416

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/01/03 13:12

ありがとうございます! def create ActiveRecord::Base.transaction do @property = Property.new(property_params) @property.save! details = create_property_details_params[:property_detail] details[:property_id] = @property.id @property_details = PropertyDetail.new(details) @property_details.save! end flash[:success] = "New property registared!" redirect_to properties_path rescue => e @property = Property.new @property_details = PropertyDetail.new flash.now[:danger] = "failed" render 'new' end にすると期待通りの処理になりました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問