##商品が出品中かどうか
アソシエーションを利用して、商品情報から購入情報を取得したい。購入情報が取得できる、できないの結果によって条件分岐したい。ご教授お願いします
ruby
1 <% if user_signed_in? %> 2 <% if current_user.id == @item.user_id %> 3 <%= link_to '商品の編集', "#", method: :get, class: "item-red-btn" %> 4 <p class='or-text'>or</p> 5 <%= link_to '削除', "#", method: :delete, class:'item-destroy' %> 6 <% elsif #商品が出品中かどうか?をどう記述したらいいかわからない %> 7 <%= link_to '購入画面に進む', "#" ,class:"item-red-btn"%> 8 <% end %>
ruby
1class ItemsController < ApplicationController 2 before_action :authenticate_user!, except: [:index, :show] 3 def index 4 @item = Item.all 5 end 6 7 def new 8 @item = Item.new 9 end 10 11 def create 12 @item = Item.new(item_params) 13 if @item.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 def show 21 @item = Item.find(params[:id]) 22 end 23 24 private 25 26 def item_params 27 params.require(:item).permit(:name, :price, :category_id, :condition_id, :postage_id, :region_id, :shipping_date_id, :description, :image).merge(user_id: current_user.id) 28 end 29 30end
ruby
1class Item < ApplicationRecord 2 extend ActiveHash::Associations::ActiveRecordExtensions 3 4 5 belongs_to :user 6 has_one :purchase 7 has_one_attached :image 8 9 with_options presence: true do 10 validates :name 11 validates :price, numericality: { greater_than_or_equal_to: 300, less_than_or_equal_to: 9_999_999 } 12 validates :category_id, numericality: { other_than: 1 } 13 validates :condition_id, numericality: { other_than: 1 } 14 validates :postage_id, numericality: { other_than: 1 } 15 validates :region_id, numericality: { other_than: 0 } 16 validates :shipping_date_id, numericality: { other_than: 0 } 17 validates :description 18 validates :image 19 end 20 21end
ruby
1class Purchase < ApplicationRecord 2 3 belongs_to :user 4 belongs_to :item 5 has_one :address 6 7end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/31 06:27