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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1796閲覧

【Rails6】中間テーブルを用いてコミュニティに参加機能を実装したい

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 6

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/02/14 16:24

解決したいこと

Rails6でコミュニティサイトを制作しており、中間テーブルを用いて、コミュニティに参加機能を実装したいと考えております。
-- 詳細 --
コミュニティ詳細ページに(参加)というボタンがあり、こちらをクリックすることでコミュニティに参加させたいです。
![イメージ説明]

解決策をご教授頂けますと幸いです。

該当するソースコード

データベース

▼community_users.rb

class CreateCommunityUsers < ActiveRecord::Migration[6.0] def change create_table :community_users do |t| t.references :user, foreign_key: true t.references :community, foreign_key: true t.timestamps end end end

モデル

▼user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :community_users, dependent: :destroy has_many :communities, through: :community_users has_many :messages has_many :coordinations has_many :comments validates :user_nickname, presence: true end

▼community.rb

class Community < ApplicationRecord has_many :community_users, dependent: :destroy has_many :users, through: :community_users has_many :messages, dependent: :destroy has_one_attached :image validates :community_title, presence: true end

▼community_user.rb

class CommunityUser < ApplicationRecord belongs_to :user belongs_to :community end

コントローラー

▼communities_controller.rb

class CommunitiesController < ApplicationController before_action :set_item, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, only: [:new, :edit, :destroy] def index @communities = Community.order("created_at DESC").page(params[:page]).per(9) end def new @community = Community.new end def create @community = Community.create(community_params) if @community.save redirect_to root_path else render :new end end def show @messages = @community.messages set_user_id_to_cookie end def edit if @community.user_id != current_user.id redirect_to new_user_session_path end end def update if @community.update(community_params) redirect_to community_path(@community.id) else render :edit end end def destroy if @community.user_id == current_user.id @community.destroy redirect_to communities_path else render :index end end private def community_params params.require(:community).permit(:community_title, :community_profile, :image).merge(user_id: current_user.id) end def set_item @community = Community.find(params[:id]) end def set_user_id_to_cookie if cookies.signed['user_id'].blank? cookies.signed['user_id'] = current_user.id end end end

ビュー

▼show.html.erb

<div class="community_button"> <% if user_signed_in? %> <% if current_user.id == @community.user_id %> <%= link_to '編集', edit_community_path(@community.id), method: :get %> <%= link_to '削除', community_path(@community.id), method: :delete %> <% else %> <div class="caution"> <%= image_tag "icon2.png" %> </div> <button>参加する</button> <% end %> <% end %> </div>

自分で試したこと

こちらからコミュニティに招待するやり方などは分かったのですが、ユーザーが自由に参加出来るようにするためにはどのように実装するのかが分かりませんでした。
上記のコードの他に必要なコードがありましたら、提示いたします。
よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

お気に入り登録を調べてみてください、例えば
お気に入り の Like を Community に置き換えて実装!

投稿2021/02/14 22:59

winterboum

総合スコア23347

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

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

退会済みユーザー

退会済みユーザー

2021/02/16 01:54

ご回答頂きありがとうございます! なるほど、お気に入り登録が同じ実装方法なんですね! お気に入り登録で考えると確かに実装出来るかもしれないです! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問