0

Railsで「お客様の商品次回購入時期を表示する」システムを作っています。

・Userテーブル:購入者情報登録済み
→カラム:id, number, name

・Productテーブル:商品情報登録済み
→カラム:id, number, brand, name, term

・Purchaseテーブル:フォームより入力
→カラム:user_number, product_number

中間テーブル(Purchase)を作成し、登録済みのuser_numberとproduct_numberを
form入力をして紐づくデータをUserテーブル,Productテーブルから取得
という機能を実装中に以下のエラーメッセージが発生しました。

NoMethodError in Purchases#index
Showing /home/ec2-user/environment/RepeatAlart_app/app/views/purchases/index.html.erb where line #18 raised:

undefined method `name' for 1:Integer
Extracted source (around line #18):
16 <tr>
17  <td><%= purchase.user_number %></td>
18  <td><%= purchase.user_number.name %></td>
19  <td><%= purchase.product_number.brand %></td>
20  <td><%= purchase.product_number.name %></td>
21  <td><%= purchase.created_at %></td> 

model

class User < ApplicationRecord    
validates :name, presence: true, length: { maximum: 50 }
validates :number, presence: true
has_many :purchases
has_many :products, through: :purchases
belongs_to :salon
end

model

class Product < ApplicationRecord
validates :brand, presence: true
validates :number, presence: true
validates :name, presence: true
validates :term, presence: true
validates :name, uniqueness: true
has_many :purchases
has_many :users, through: :purchases
belongs_to :salon
end

model

class Purchase < ApplicationRecord
    belongs_to :user, optional: true
    belongs_to :product, optional: true
    belongs_to :salon
end

controller

class PurchasesController < ApplicationController
  def index
    @purchases = Purchase.where(salon_id: current_salon.id)
    @purchase = Purchase.new
  end

  def create
    @purchase= Purchase.new(purchase_params.merge(salon_id: current_salon.id))
    if @purchase.save
      flash[:success] = "Success!"
      redirect_to "/purchases"
    else
      redirect_to "/purchases"
    end
  end

  def edit
      @purchase=Purchase.find(params[:id])
  end

  def update
      purchase=Purchase.find(params[:id])
      purchase.update(purchase_params)
      redirect_to "/purchases"
  end 

  def destroy
     Purchase.find(params[:id]).destroy
    flash[:success]= "deleted"
    redirect_to "/purchases"
  end

   private

     def purchase_params
      params.require(:purchase).permit(:user_number, :product_number)
     end  
end

view

<h1>購入商品一覧</h1>
        <table class="table">
            <thead>
                <tr>
                   <th>お客様No.</th>
                   <th>お客様名</th>
                   <th>ブランド</th>
                   <th>商品名</th>
                   <th>購入日</th>
                   <th>次回購入推奨日</th>
                    <th colspan="2"></th>
                </tr>
            </thead>
            <tbody>
                    <% @purchases.each do |purchase| %>
                <tr>
                    <td><%= purchase.user_number %></td>
                    <td><%= purchase.user_number.name %></td>
                    <td><%= purchase.product_number.brand %></td>
                    <td><%= purchase.product_number.name %></td>
                    <td><%= purchase.created_at %></td>
                    <td><%= purchase.limit_date %></td>
                    <td><%= link_to "編集", "#", class:"btn btn-primary mr-3" %></td>
                    <td><%= link_to "削除", purchase, method: :delete, class:"btn btn-primary mr-3" %></td>
                    <% end %>
                <tr>
             </tbody>
        </table>      

<h1>購入情報登録</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@purchase) do |f| %>
      <%= f.label :user_number %>
      <%= f.number_field :user_number, class: 'form-control' %>
      <%= f.label :product_number %>
      <%= f.number_field :product_number, class: 'form-control' %>

      <%= f.submit "購入情報登録", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

1 件の回答 1

1

index.html.erb内の
purchase.user_number.name

purchase.user.name
などに修正してみてください。

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。