前提・実現したいこと
記事を削除した際に、多対多であるタグ情報も一緒に削除できるようにしたいです。
他のサイトを見てみても、同じような記述で出来ているようですが。。。
どなたかご教授いただければと思います。
宜しくお願いいたします。
発生している問題・エラーメッセージ
Processing by Users::ArticlesController#destroy as HTML Parameters: {"authenticity_token"=>"9NYI/LWmMSLANHDVSwlxx+THFq4aiEf6UIBPOlfHL2g7s8x+WyeuF5aShwLvCn0OC7+rAF3HWJiu6zBhacQ+og==", "id"=>"47"} Article Load (0.4ms) SELECT `articles`.* FROM `articles` WHERE `articles`.`id` = 47 LIMIT 1 ↳ app/controllers/users/articles_controller.rb:50 (0.1ms) BEGIN ↳ app/controllers/users/articles_controller.rb:51 Impression Destroy (0.3ms) DELETE FROM `impressions` WHERE `impressions`.`impressionable_id` = 47 AND `impressions`.`impressionable_type` = 'Article' ↳ app/controllers/users/articles_controller.rb:51 TagMap Load (0.4ms) SELECT `tag_maps`.* FROM `tag_maps` WHERE `tag_maps`.`article_id` = 47 ↳ app/controllers/users/articles_controller.rb:51 TagMap Destroy (0.3ms) DELETE FROM `tag_maps` WHERE `tag_maps`.`article_id` = 47 AND `tag_maps`.`tag_id` = 1 ↳ app/controllers/users/articles_controller.rb:51 TagMap Destroy (0.3ms) DELETE FROM `tag_maps` WHERE `tag_maps`.`article_id` = 47 AND `tag_maps`.`tag_id` = 2 ↳ app/controllers/users/articles_controller.rb:51 TagMap Destroy (0.3ms) DELETE FROM `tag_maps` WHERE `tag_maps`.`article_id` = 47 AND `tag_maps`.`tag_id` = 3 ↳ app/controllers/users/articles_controller.rb:51 Article Destroy (0.7ms) DELETE FROM `articles` WHERE `articles`.`id` = 47 ↳ app/controllers/users/articles_controller.rb:51 User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 ↳ app/controllers/users/articles_controller.rb:51 (5.6ms) ROLLBACK ↳ app/controllers/users/articles_controller.rb:51 Completed 500 Internal Server Error in 34ms (ActiveRecord: 9.3ms) TypeError (nil is not a symbol nor a string): app/controllers/users/articles_controller.rb:51:in `destroy'
該当のソースコード
/tag.rb class Tag < ApplicationRecord validates :tag_name, presence: true, length: {maximum:99} has_many :tag_maps, dependent: :destroy has_many :articles, through: :tag_maps end
/article.rb class Article < ApplicationRecord mount_uploader :article_thumbnail, ArticleThumbnailUploader is_impressionable counter_cache: true belongs_to :user, dependent: :destroy belongs_to :category belongs_to :sub_category belongs_to :user, optional: true has_many :tag_maps, dependent: :destroy has_many :tags, through: :tag_maps accepts_nested_attributes_for :tag_maps, allow_destroy: true
/tag_map.rb class TagMap < ApplicationRecord belongs_to :article belongs_to :tag end
スキーマ create_table "articles", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.text "article_title", null: false t.text "article_content", null: false t.integer "user_id", null: false t.integer "category_id" t.integer "sub_category_id" t.string "article_thumbnail" t.integer "impressions_count", default: 0 t.datetime "created_at", null: false t.datetime "updated_at", null: false end
コントローラー class Users::ArticlesController < ApplicationController before_action :require_login before_action :logedin_admin? def show @article = Article.find(params[:id]) end def new @article = Article.new end def edit @article = Article.find(params[:id]) end def create @article = Article.new(article_params) respond_to do |format| begin ActiveRecord::Base.transaction do @article.save! end # 正常処理 format.html { redirect_to articles_path } format.json { render :index, location: @article } rescue ActiveRecord::RecordInvalid # バリデーション処理 @article.valid? format.html { render :new } format.json { render json: @article.errors } rescue => e Rails.logger.error e Rails.logger.error e.backtrace.join("\n") Raven.capture_exception(e) flash.now[:danger] = "保存に失敗しました" format.html { render :new } end end end def update @article = Article.find(params[:id]) if @article.update!(article_params) redirect_to root_url else render :edit end end def destroy @article = Article.find(params[:id]) @article.destroy! redirect_to users_articles_path end private def article_params params.require(:article).permit(:article_title, :article_content, :article_thumbnail, :article_thumbnail_cache, :category_id, :sub_category_id, {:tag_ids => []}, :user_id) end def logedin_admin? redirect_to articles_path unless current_user.admin? end def set_user end end
試したこと
ActiveRecordでidがないテーブルにデータを保存しようとすると"nil is not a symbol"とか言われる
補足情報(FW/ツールのバージョンなど)
ruby 2.5.2p104 (2018-10-18 revision 65133) [x86_64-linux]
Rails 5.2.1
ここにより詳細な情報を記載してください。
DBのスキーマ情報や、Users::ArticlesController の中身があれば回答が得られやすいと思います
遅くなって申し訳ございません!情報追加しました。
宜しくお願いいたします。。。
もう少し細かいログがみたいです。--traceとかで出力できたような
それで現状のログを見たいのと
試しに
has_many :tag_maps # , dependent: :destroy
これで実行した場合の結果をみたいです。
ここも気になりますね。
class Article
belongs_to :user, dependent: :destroy
belongs_to :user, optional: true
回答1件
あなたの回答
tips
プレビュー