前提・実現したいこと
railsでイベントマッチングのサイトを作っています。eventsテーブルが投稿内容で、entriesテーブルが申し込んだ内容となっています。userが申し込んだ内容を表示したいのですがうまく表示することができません。
該当のソースコード
routes.rb
Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'users/registrations', omniauth_callbacks: 'users/omniauth_callbacks' } get 'entries/index' root to: 'events#index' resources :events do collection do get 'search' end resources :entries, only:[:new, :create] resource :likes, only:[:create, :destroy] end resources :users, only: [:show, :new] do member do get 'mypage' end end end
users.controller.rb
class UsersController < ApplicationController before_action :authenticate_user!, only: [:mypage] before_action :set_user, only:[:show, :mypage] before_action :move_to_index_edit, only:[:mypage] def show @events = @user.events.order("created_at DESC").page(params[:page]).per(6) @entries = @user.entries.order("created_at DESC").page(params[:page]).per(6) end def mypage end def new end private def set_user @user = User.find(params[:id]) end def move_to_index_edit if current_user.id != @user.id redirect_to root_path end end end
events.controller.rb
class EventsController < ApplicationController before_action :authenticate_user!, only:[:new, :create, :edit, :update, :destroy] before_action :event_id_params, only:[:show, :edit, :update, :destroy] before_action :move_to_index_edit, only:[:edit, :destroy] before_action :search_event, only:[:index, :search] def index @events = Event.order("created_at DESC").page(params[:page]).per(6).includes(:user) set_event_column end def new @event = Event.new end def create @event = Event.new(event_params) if @event.save redirect_to root_path, notice: 'イベントの投稿が完了しました' else render :new end end def show end def edit end def update if @event.update(event_params) redirect_to event_path, notice: 'イベントの編集が完了しました' else render :edit end end def destroy @event.destroy redirect_to root_path, notice: 'イベントが消去されました' end def search @events = @p.result.includes(:user) @events = @events.order("created_at DESC").page(params[:page]).per(6).includes(:user) end private def event_params params.require(:event).permit(:name, :image, :explanation, :genre_id, :day, :start_time_id, :end_time_id, :capacity, :area_id, :address).merge(user_id: current_user.id) end def event_id_params @event = Event.find(params[:id]) end def move_to_index_edit if current_user.id != @event.user_id redirect_to root_path end end def search_event @p = Event.ransack(params[:q]) set_event_column end def set_event_column @events_genre = Genre.where.not(id:1) @events_area = Area.where.not(id:1) end end
entries.controller.rb
class EntriesController < ApplicationController before_action :authenticate_user! before_action :set_event before_action :move_to_index_entry def new @entry = Entry.new end def create @entry = Entry.new(entry_params) if @entry.valid? @entry.save redirect_to root_path, notice: 'イベント申し込みが送信されました' else render action: :index end end private def set_event @event = Event.find(params[:event_id]) end def entry_params params.require(:entry).permit(:user_id, :event_id).merge(user_id: current_user.id, event_id: params[:event_id]) end def move_to_index_entry if current_user.id == @event.user_id || @event.id.blank? redirect_to root_path end end end
users テーブル
Column | Type | Options |
---|---|---|
nickname | string | null: false |
string | null: false, unique: true | |
encrypted_password | string | null: false |
family_name | string | null: false |
first_name | string | null: false |
kana_family_name | string | null: false |
kana_first_name | string | null: false |
birth_day | date | null: false |
area_id | integer | null: false |
phone_number | string | null: false |
introduction | string |
Association
- has_many :events
- has_many :entries
- has_many :sns_credentials
- belongs_to :area
events テーブル
Column | Type | Options |
---|---|---|
name | string | null: false |
explanation | text | null: false |
genre_id | string | null: false |
day | date | null: false |
start_time_id | integer | null: false |
end_time_id | integer | null: false |
latitude | float | |
longitude | float | |
capacity | string | null: false |
area_id | integer | null: false |
address | string | null: false |
Association
- belongs_to :user
- has_many :entries
- belongs_to :genre
- belongs_to :area
- belongs_to :start_time
- belongs_to :end_time
entries テーブル
Column | Type | Options |
---|---|---|
event | references | null: false, foreign_key: true |
user | references | null: false, foreign_key: true |
Association
- belongs_to :user
- belongs_to :event
補足情報(FW/ツールのバージョンなど)
- Ruby 2.6.5
- Ruby on Rails 6.0.3.4
- MySQL 5.6
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。