一対多の関係にあるデータベースを一覧として表示したいのですが index.html.erb
に子テーブルの情報を表示させる方法がわかりません。分かる方いましたらよろしくお願いします。
user.schedules
で子テーブルの情報を引っ張ってくるところまではできたんですが user.schedules.time
などの個々のカラム情報を取り出そうとすると undefined method `time' となってしまい取り出すことができませんでした。
Rails ver は5.0.7.2です。
親:User テーブル (company,userid,pass,memo)
子:Schedule テーブル (time,user_id,category_id)
Category テーブル (category)
ruby
1#index.html.erb 2 3<p id="notice"><%= notice %></p> 4 5<h1>Users</h1> 6 7<table> 8 <thead> 9 <tr> 10 <th>企業名</th> 11 <th>ユーザーID</th> 12 <th>パスワード</th> 13 <th>メモ</th> 14 <th colspan="3"></th> 15 </tr> 16 </thead> 17 18 <tbody> 19 <% @users.each do |user| %> 20 <tr> 21 <td><%= user.company %></td> 22 <td><%= user.userid %></td> 23 <td><%= user.pass %></td> 24 <td><%= user.memo %></td> 25 <td><%= link_to 'Show', user %></td> 26 <td><%= link_to 'Edit', edit_user_path(user) %></td> 27 <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> 28 </tr> 29 30 <% end %> 31 </tbody> 32</table> 33 34
ruby
1#users_controller.rb 2 3class UsersController < ApplicationController 4 before_action :set_user, only: [:show, :edit, :update, :destroy] 5 6 7 # GET /users 8 # GET /users.json 9 def index 10 @users=User.all 11 end 12 13 # GET /users/1 14 # GET /users/1.json 15 def show 16 end 17 18 # GET /users/new 19 def new 20 @user = User.new 21 @schedule = @user.schedules.build 22 @categories=Category.all 23 end 24 25 # GET /users/1/edit 26 def edit 27 end 28 29 # POST /users 30 # POST /users.json 31 def create 32 @user = User.new(user_params) 33 34 respond_to do |format| 35 if @user.save 36 format.html { redirect_to @user, notice: 'User was successfully created.' } 37 format.json { render :show, status: :created, location: @user } 38 else 39 format.html { render :new } 40 format.json { render json: @user.errors, status: :unprocessable_entity } 41 end 42 end 43 end 44 45 # PATCH/PUT /users/1 46 # PATCH/PUT /users/1.json 47 def update 48 respond_to do |format| 49 if @user.update(user_params) 50 format.html { redirect_to @user, notice: 'User was successfully updated.' } 51 format.json { render :show, status: :ok, location: @user } 52 else 53 format.html { render :edit } 54 format.json { render json: @user.errors, status: :unprocessable_entity } 55 end 56 end 57 end 58 59 # DELETE /users/1 60 # DELETE /users/1.json 61 def destroy 62 @user.destroy 63 respond_to do |format| 64 format.html { redirect_to users_url, notice: 'User was successfully destroyed.' } 65 format.json { head :no_content } 66 end 67 end 68 69 private 70 # Use callbacks to share common setup or constraints between actions. 71 def set_user 72 @user = User.find(params[:id]) 73 end 74 75 # Only allow a list of trusted parameters through. 76 def user_params 77 params.require(:user).permit(:company, :userid, :pass, :memo,schedules_attributes: [:id, :time, :user_id, :category_id,:_destroy ] ) 78 end 79end
ruby
1 2#user.rb 3class User < ApplicationRecord 4 has_many :schedules, dependent: :destroy 5 accepts_nested_attributes_for :schedules, allow_destroy: true 6 7end 8 9 10#schedule.rb 11class Schedule < ApplicationRecord 12 belongs_to :user 13end 14 15 16#category.rb 17class Category < ApplicationRecord 18end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/16 13:40
2020/05/16 13:46
2020/05/16 15:26
2020/05/16 22:00
2020/05/17 05:21
2020/05/17 06:41
2020/05/17 07:03