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

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

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

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

Q&A

解決済

1回答

1331閲覧

rails グループ機能の管理者を予めグループユーザーにする方法

popi06

総合スコア3

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/14 14:50

編集2021/04/15 08:58

railsでユーザーが自由にグループに入ることができるグループ機能を制作しています。

1グループ(circle)を作成したら、作成したユーザーがオーナーになる。
2グループオーナーは予めグループユーザーである 

2番目のグループオーナーを予めグループユーザーにする方法がわかりません。
初学者なのでわかりやすく教えていただきたいです。

/app/controllers/circles_controller.rb class CirclesController < ApplicationController before_action :set_circle, only: [:update, :destroy, :edit] before_action :set_parents, only: [:new, :create, :destroy] before_action :owner_user, only: [:edit, :destroy] def index @circles = Circle.paginate(page: params[:page]).search(params[:search]) end def member @circle_user = CircleUser.paginate(page: params[:page]) end def new @circle = Circle.new if !@circle.user.include?(current_user) @circle.user << current_user end end def show @circle = Circle.find(params[:id]) @user = User.find(params[:id]) @circleposts = Circlepost.where(circle_id: @circle.id).all end def create @circle = Circle.new(circle_params) @circle.owner = current_user ※一番目の処理 if @circle.save @circle_user = @circle.owner flash[:success] = "作成しました" redirect_to root_url else render :new end end def edit @circle = Circle.find(params[:id]) end def update @circle = Circle.find(params[:id]) if @circle.update(circle_params) flash[:success] = "サークル情報が更新されました" redirect_to @circle else render 'edit' end end def destroy @circle = Circle.find_by(id: params[:id]) return redirect_to :root if @circle.nil? @circle.destroy flash[:success] = 'サークルを削除しました。' redirect_to :root end def search if params[:name].present? @circles = Circle.where('name LIKE ?', "%#{params[:name]}%") else @circles = Circle.none end end def set_parents @parents = Category.where(ancestry: nil) end def get_category_children @category_children = Category.find("#{params[:parent_id]}").children end def get_category_grandchildren @category_grandchildren = Category.find("#{params[:child_id]}").children end ・・・ private def owner_user redirect_to(root_url) unless current_user && @circle.owner? end def circle_params params.require(:circle).permit(:categries_id, :category_grandchildren, :category_children, :parent_id, :name, :place, :time, :homepage, :profile, { :user_ids => [] }) end def set_circle @circle = Circle.find(params[:id]) end end

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

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

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

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

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

guest

回答1

0

自己解決

グループユーザーのcreateをグループ作成時に記入しました

投稿2021/05/04 15:46

popi06

総合スコア3

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問