0

我試圖做這個person做了什麼,但與rails 5.如何鏈接到rails 5中的父關聯?

在我的應用程序,我有兩個控制器和三個模型。

我的兩個控制器是OrdersControllerListingsController。我的三個型號是OrderListingUser

我正在嘗試在我的OrdersController內的我的銷售和購買頁面上創建用戶關聯鏈接,該鏈接指向我的ListingsController中的#show操作。

,我有我的銷售和購買頁面看起來像這樣的鏈接:

<% @orders.each do |order| %> 
    <tr> 
    <td class="center"><%= link_to 'View The Game Your Sold', listing_path(@listing) %></td> 
    </tr> 
<% end %> 

這是我Order型號:

belongs_to :listing 
belongs_to :buyer, class_name: "User" 
belongs_to :seller, class_name: "User" 

這是我OrdersController

class OrdersController < ApplicationController 
    before_action :set_order, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 

    def sales 
    @orders = Order.all.where(seller: current_user).order("created_at DESC") 
    end 

    def purchases 
    @orders = Order.all.where(buyer: current_user).order("created_at DESC") 
    end 

    # GET /orders/new 
    def new 
    @order = Order.new 
    @listing = Listing.find(params[:listing_id]) 
    end 
end 

這是我的Listing型號:

belongs_to :user 
has_many :orders 
has_many :fullgames 

def self.search(params) 
    listings = all # for not existing params args 
    listings = listings.where("name like ?", "%#{params[:name]}%") if params[:name] 
    listings 
end  

這是我ListingsController

class ListingsController < ApplicationController 
    before_action :set_listing, only: [:show, :edit, :update, :destroy] 
    before_filter :authenticate_user!, only: [:seller, :new, :create, :edit, :update, :destroy] 
    before_filter :check_user, only: [:edit, :update, :destroy] 

    def seller 
    @listings = Listing.where(user: current_user).order("created_at DESC") 
    end 

    # GET /listings 
    # GET /listings.json 
    def index 
    @listings = Listing.all.order("created_at DESC").search(params).paginate(:page => params[:page], :per_page => 2) 
    end 

    # GET /listings/1 
    # GET /listings/1.json 
    def show 
    end 
end 

這是我User型號:

has_many :listings, dependent: :destroy 
has_many :sales, class_name: "Order", foreign_key: "seller_id" 
has_many :purchases, class_name: "Order", foreign_key: "buyer_id" 
+0

你的問題太長,我沒有得到你。請澄清更多關於您的問題 – Vishal

+0

app/views/orders/sales.html.erb(我有一個銷售名單,每個用戶已作出) app/views/orders/purchases.html.erb(我有一個列表每個用戶進行購買) 應用程序/視圖/列表/ index.html.erb(顯示所有項目的列表) 應用程序/視圖/列表/ show.html.erb(在indivivually項顯示) 我想讓它成爲我的用戶可以點擊銷售頁面中的商品併購買,並讓它鏈接到 顯示頁面中的該商品。 – AaronG

回答

0
# Order Model 
class Order < ApplicationRecord 
    scope :recent, -> { order("created_at DESC") } 
end 

# ListingsController 
class ListingsController < ApplicationController 
    ... 
    before_filter :authenticate_user!, only: [:show, :seller, :new, :create, :edit, :update, :destroy] 

    # GET /listings/1 
    # GET /listings/1.json 
    def show 
    @purchases = current_user.purchases.recent 
    @sales = current_user.sales.recent 
    end 
end 

# show.html.erb 
# for purchases orders 
<% @purchases.each do |purchase_order| %> 
    <tr> 
    <td class="center"><%= purchase_order %></td> 
    </tr> 
<% end %> 

# for sales orders 
<% @sales.each do |sale_order| %> 
    <tr> 
    <td class="center"><%= sale_order %></td> 
    </tr> 
<% end %>