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

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

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

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

Q&A

解決済

1回答

334閲覧

中間テーブルにデータが保存されるようにしたい

shawn_709

総合スコア13

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/21 06:39

ご回答いただけるとありがたいです。よろしくお願いします!

前提・実現したいこと

新規グループを作成した時に、中間テーブル(group_users)に情報が保存されるようにしたい。
userはdeviseを用いている。

発生している問題・エラーメッセージ

現在、新規グループ作成ページで必要な情報を入力して送信すれば、groupsテーブルに情報が保存される。
しかし、中間テーブルであるgroup_usersテーブルには何も保存されない。
※エラーメッセージが出るわけではない。

該当のソースコード

  • routes.rb
Rails.application.routes.draw do devise_for :users root to: "tweets#index" resources :groups, only: [:new, :create] end
  • migration
class CreateGroups < ActiveRecord::Migration[6.0] def change create_table :groups do |t| t.string :name, null: false, unique: true t.string :content t.timestamps end end end
class CreateGroupUsers < ActiveRecord::Migration[6.0] def change create_table :group_users do |t| t.references :group, foreign_key: true t.references :user, foreign_key: true t.timestamps end end end
  • model
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :nickname, presence: true has_many :group_users has_many :groups, through: :group_users end
class Group < ApplicationRecord has_many :group_users has_many :users, through: :group_users validates :name, presence: true end
class GroupUser < ApplicationRecord belongs_to :group belongs_to :user end
  • controller
class GroupsController < ApplicationController def new @group = Group.new @group.users << current_user end def create @group = Group.new(group_params) if @group.save redirect_to root_path else render :new end end private def group_params params.require(:group).permit(:name, :content, user_ids: []) end end
  • views/groups/new.html.erb
<div class='chat-room-form'> <h1>新規グループ作成</h1> <%=form_with model: @group, local: true do |f|%> <div class='chat-group-form__field'> <div class='chat-group-form__field--left'> <%= f.label :グループ名, class: 'chat-group-form__label'%> </div> <div class='chat-group-form__field--right'> <%= f.text_field :name, class: 'chat__group_name chat-group-form__input', placeholder: 'グループ名を入力してください'%> </div> </div> <div class='chat-group-form__field'> <div class='chat-group-form__field--left'> <%= f.label :グループ内容, class: 'chat-group-form__label'%> </div> <div class='chat-group-form__field--right'> <%= f.text_field :content, class: 'chat__group_name chat-group-form__input', placeholder: 'グループの内容を入力してください'%> </div> </div> <div class='chat-group-form__field'> <div class='chat-group-form__field--left'></div> <div class='chat-group-form__field--right'> <%= f.submit class: 'chat-group-form__action-btn'%> </div> </div> <% end %> </div>

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

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

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

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

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

guest

回答1

0

自己解決

groupコントローラーのcreateアクションを以下のように編集したら、解決できました。
調べてみて、試したら解決できただけで、どういう理由でこうなっているかはわかっていません。

def create @group = current_user.groups.new(group_params) if current_user.save redirect_to root_path else render :new end end

投稿2020/11/21 09:00

shawn_709

総合スコア13

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問