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

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

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

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby

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

Ruby on Rails

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

Q&A

解決済

1回答

1307閲覧

group_idが取得できない

risarisa.373

総合スコア0

Haml

Haml(HTML abstraction markup language)は、HTML/XHTMLを効率的に記述するためのマークアップ言語および記法です。

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/07/30 07:56

前提・実現したいこと

group_idの取得

ユーザー登録したら、グループ登録できる仕様なのですが
グループ登録がcreateされる前にエラーの画面が出てしまって
group_idが取得できていないとの内容でした。

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

https://gyazo.com/7a70f0952a285d231f2487f7fcde618b

該当のソースコード

schema.rb create_table "contents", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.text "text" t.text "image" t.bigint "group_id" t.bigint "user_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["group_id"], name: "index_contents_on_group_id" t.index ["user_id"], name: "index_contents_on_user_id" end create_table "group_users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.bigint "group_id" t.bigint "user_id" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["group_id"], name: "index_group_users_on_group_id" t.index ["user_id"], name: "index_group_users_on_user_id" end create_table "groups", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false end create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| t.string "name" t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end add_foreign_key "contents", "groups" add_foreign_key "contents", "users" add_foreign_key "group_users", "groups" add_foreign_key "group_users", "users" end routes.rb Rails.application.routes.draw do devise_for :users root "groups#new" resources :users, only: [:index, :new, :create, :destroy] resources :groups, only: [:new, :create, :edit, :update] do resources :contents do resources :comments, only: :create collection do get 'search' end end end end /contents_controller.rb class ContentsController < ApplicationController before_action :set_group def index @content = Content.new @contents = Content.includes(:user).order("created_at DESC") end def new @content = Content.new @group.users << current_user end def create @content = @group.contents.new(content_params) if @content.save respond_to do |format| format.json end else @contents = @group.contents.includes(:user) flash.now[:alert] = 'メッセージを入力してください。' render :index end end def destroy content = Content.find(params[:id]) content.destroy redirect_to group_contents_path end def edit @content = Content.find(params[:id]) end def update content = Content.find(params[:id]) content.update(content_params) end def show @comment = Comment.new @comments = @content.comments.includes(:user) end def search @contents = Content.search(params[:keyword]) # respond_to do |format| # format.html # format.json # end end private def content_params params.require(:message).permit(:content, :image).merge(user_id: current_user.id) end def set_group @group = Group.find(params[:group_id]) end end groups_controller.rb class GroupsController < ApplicationController # before_action :set_group def index end def new @group = Group.new @group.users << current_user end def create @group = Group.new(group_params) if @group.save redirect_to group_contents_path, notice: 'グループを作成しました' else render :new end end def edit @group = Group.find(params[:id]) end def update @group = Group.find(params[:id]) if @group.update(group_params) redirect_to group_messages_path(@group), notice: 'グループを更新しました' else render :edit end end private def group_params params.require(:group).permit(:name, user_ids: []) end # def set_group # @group = Group.find(params[:group_id]) # end end users_controller.rb class UsersController < ApplicationController before_action :user_sign_in def index return nil if params[:keyword] == "" @users = User.where(['name LIKE ?', "%#{params[:keyword]}%"] ).where.not(id: current_user.id).limit(10) end def new @user = User.new end def create @users = @user if current_user.Create.new(user_params) redirect_to new_group_path else redirect_to new_user_registration_path end end def show user = User.find(params[:id]) @contents = user.contents end private def user_params params.require(:user).permit(:name, :email) end def user_log_in @group_new = redirect_to new_group_path end end

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

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

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

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

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

winterboum

2020/07/30 20:42

modelの関連定義のところを載せてください
guest

回答1

0

自己解決

controller、変数などでうまく渡せていなかったのが原因でした!!

投稿2020/08/02 22:44

risarisa.373

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問