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

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

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

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

Q&A

解決済

1回答

864閲覧

データベースに値が保存されない

taba.3011

総合スコア12

Ruby on Rails

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

0グッド

0クリップ

投稿2021/01/13 14:52

フォームオブジェクトで、投稿内容とタグを別のDBに保存しようとしています。
実際に投稿してみると、タグはDBに保存されているのですが、投稿の内容が保存されません。
タグが保存されているので構文はあっていると思い、投稿内容に関する箇所を一通り確認していきましたが問題が分かりません。
##コード

[articles_controller] def new @article = ArticlesTag.new end def create # binding.pry @article = ArticlesTag.new(article_tag_params) if @article.valid? @article.save return redirect_to root_path else render "new" end end private def article_tag_params params.require(:articles_tag).permit(:name, :title, :content).merge(user_id: current_user.id) end end
[articles_tag.rb] class ArticlesTag include ActiveModel::Model attr_accessor :title, :content, :name, :user_id with_options presence: true do validates :title validates :content validates :name validates :user_id end def save # binding.pry article = Article.create(title: title, content: content) tag = Tag.where(name: name).first_or_initialize tag.save ArticleTagRelation.create(article_id: article.id, tag_id: tag.id) end end
[new.html.rb] <div class="article-wrapper"> <%= form_with model: @article, url: articles_path, local: true do |f| %> <div calss="article-title"> <%= f.text_field :title, id: "article-title", placeholder:"50文字以内", size:'50' %> </div> <div calss="article-tag"> <%= f.text_field :name, id: "article-tag",placeholder:"株に関連するタグをスペース区切りで3つまで入力 (例: 株価 インフレ)", size:'70', id: "article-tag"%> </div> <div calss="article-text"> <%= f.text_area :content, placeholder:"株の知識を共有しよう", id: "article-content" %> </div> <div calss="article-post"> <%= f.submit "投稿する", class:"post-btn" %> </div> <% end %> </div>
[DB article] class CreateArticles < ActiveRecord::Migration[6.0] def change create_table :articles do |t| t.string :title, null: false t.string :content, null: false t.references :user, foreign_key: true t.timestamps end end end
[DB tag] class CreateTags < ActiveRecord::Migration[6.0] def change create_table :tags do |t| t.string :name, null: false, uniqueness: true t.timestamps end end end
[DB article_tag_relation] class CreateArticleTagRelations < ActiveRecord::Migration[6.0] def change create_table :article_tag_relations do |t| t.references :article, foreign_key: true t.references :tag, foreign_key: true t.timestamps end end end
[model article] class Article < ApplicationRecord has_many :article_tag_relations has_many :tags, through: :article_tag_relations belongs_to :user end
[model tag] class Tag < ApplicationRecord has_many :article_tag_relations has_many :articles, through: :article_tag_relations validates :name, uniqueness: true end
[model article_tag_relation] class ArticleTagRelation < ApplicationRecord belongs_to :article, optional: true belongs_to :tag, optional: true end

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

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

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

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

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

m.ts10806

2021/01/13 19:39

ArticlesTag しかnew→saveしてないように見受けられますが、そこは認識ありますか?
taba.3011

2021/01/14 02:22

m.ts10806様コメントありがとうございます。 articlesテーブルにuser_idを追加しており、そのuser_idを保存する処理を記述していなかったことが原因でした。自分の凡ミスでした。 ArticlesTagは、ArticlesTagをnew→saveで[articles_tag.rb]のsaveメソッドを呼び出して、articleとtagをまとめて保存しているという認識です。もし齟齬があればご指摘ください。
guest

回答1

0

自己解決

##流れ

コントローラー def create @article = ArticlesTag.new(article_tag_params) if @article.valid? @article.save return redirect_to root_path else render "new" end end

###↓↓↓

フォームオブジェクト def save article = Article.create(title: title, content: content, **user_id: user_id**, image: image) tag = Tag.where(name: name).first_or_initialize tag.save ArticleTagRelation.create(article_id: article.id, tag_id: tag.id) end

##解決
フォームオブジェクトのArticle.createで、user_idを含めていないことが原因で値が保存されていませんでした。

投稿2021/01/14 02:10

taba.3011

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問