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

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

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

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

Ruby on Rails

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

Q&A

1回答

1089閲覧

グループ申請機能がうまく表示されない・所属できない

popi06

総合スコア3

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/13 13:31

編集2022/01/12 10:55

railsで下記の記事を参考にグループ機能を制作しているのですが、グループに申請してグループユーザーになるという流れの機能において、加入申請ボタンを押しても申込取り消しボタンに切り替わりません。また、申請を承認しても「退会する」ボタンは表示されず、加入申込が表示されたままです。
初学者なのでわかりやすく、どの点を修正した方が良いか教えていただきたいです。

発生した問題

/app/views/circles/show.html.erb ・・・ <div class="enrollment"> <% if @circle_user %> <%= link_to '退会する', circle_circle_user_path(@circle, @circle_user), method: :delete, data:{ confirm: "コミュニティ「#{@circle.name}」を退会します。よろしいですか?" } ,class:"btn btn-warning btn-lg" %> <!--サークルには所属していないが、ログインはしている場合 --> <% elsif current_user %> <% if @apply %> <%= link_to '申請取消', circle_apply_path(@circle, @apply), method: :delete, class:"btn btn-warning btn-lg" %> <% else %> <%= link_to '加入申請', circle_applies_path(@circle), method: :post, class:"btn btn-warning btn-lg" %> <% end %> <% end %> <% if @circle.owner? %> <%= link_to "編集", edit_circle_path(@circle), class:"btn btn-warning btn-lg" %> <%= link_to "削除", circle_path, method: :delete, class:"btn btn-warning btn-lg", data: { confirm: "(確認)サークルが削除されます。" } %> <% end %> </div>
/app/controllers/applies_controller.rb class AppliesController < ApplicationController def create current_user.applies.create(circle_id: apply_params[:circle_id]) redirect_to circle_url(apply_params[:circle_id]) flash[:success] = "加入申請しました" end def destroy @apply = Apply.find(params[:id]) @apply.destroy! @circle = Circle.find(params[:circle_id]) redirect_to circle_url(@circle) flash[:info] = "加入申請を取り消しました" end def index @applies = Apply.where(circle_id: params[:circle_id]) end def show end private def apply_params params.permit(:circle_id) end end
/app/controllers/circle_users_controller.rb class CircleUsersController < ApplicationController def index @cicle_users = CircleUser.paginate(page: params[:page]) end def create @circle_user = CircleUser.create(circle_id: circle_user_params[:circle_id], user_id: circle_user_params[:user_id]) Apply.find(circle_user_params[:apply_id]).destroy! redirect_to circle_applies_url(@circle_user.circle) flash[:success] = "「#{@circle_user.user.name}」が、サークル:#{@circle_user.circle.name}へ加入しました。" end def destroy @circle_user = CircleUser.find(params[:id]) @circle_user.destroy! @circle = Circle.find(params[:circle_id]) redirect_to circle_url(@circle), notice: "サークル「#{@circle.name}」を退会しました。" end def show @circle_user = CircleUser.find(params[:id]) end private def circle_user_params params.permit(:circle_id, :user_id, :apply_id) end end
/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 @circle.user << current_user end def show @circle = Circle.find(params[:id]) if !@circle.user.include?(current_user) @circle.user << current_user end @circleposts = Circlepost.where(circle_id: @circle.id).all end def create @circle = Circle.new(circle_params) @circle.owner = current_user if @circle.save flash[:success] = "作成しました" redirect_to circle_users_path 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.find(id: params[:id]) if @circle.destroy flash[:success] = "サークルを削除しました" else flash[:danger] = '削除に失敗しました。' redirect_to circle_url end 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 def top respond_to do |format| format.html format.json do if params[:parent_id] @childrens = Category.find(params[:parent_id]).children elsif params[:children_id] @grandChilds = Category.find(params[:children_id]).children elsif params[:gcchildren_id] @parents = Category.where(id: params[:gcchildren_id]) end end end end private def owner_user redirect_to(root_url) unless current_user && @circle.owner? end def circle_params params.require(:circle).permit(:name, :place, :time, :homepage, :profile, { :user_ids => [] }) end def set_circle @circle = Circle.find(params[:id]) end end

参考にした記事

https://qiita.com/tttaamm12/items/fbc2a3723ce3077adcbc#%E5%AE%9F%E6%96%BD%E3%81%97%E3%81%9F%E3%81%84%E3%81%93%E3%81%A8

https://qiita.com/savaniased/items/ce7dd5a825ad0f6be53c

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

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

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

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

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

guest

回答1

0

/app/controllers/circle_users_controller.rb
では

def show @circle_user = CircleUser.find(params[:id]) end

で@circle_userが定義されてますが、

/app/controllers/circles_controller.rb
では

def show @circle = Circle.find(params[:id]) if !@circle.user.include?(current_user) @circle.user << current_user end @circleposts = Circlepost.where(circle_id: @circle.id).all end

で、@circle.userしか定義してないから
<% if @circle_user %>
の分岐に引っかからないとか?

投稿2021/04/24 04:44

moonlight4_6_17

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問