How do I link to parent association in rails 5? -


i trying person did rails 5.

in app have 2 controllers , 3 models.

my 2 controllers orderscontroller , listingscontroller. , 3 models order, listing, , user.

i trying create user-associated links on sales , purchases pages within orderscontroller link #show action within listingscontroller.

the links have on sales , purchases page this:

<% @orders.each |order| %>   <tr>     <td class="center"><%= link_to 'view game sold', listing_path(@listing) %></td>   </tr> <% end %> 

this order model:

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

this 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    # /orders/new   def new     @order = order.new     @listing = listing.find(params[:listing_id])   end end 

this listing model:

belongs_to :user has_many :orders has_many :fullgames  def self.search(params)   listings = # not existing params args   listings = listings.where("name ?", "%#{params[:name]}%") if params[:name]   listings end      

this 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    # /listings   # /listings.json   def index     @listings = listing.all.order("created_at desc").search(params).paginate(:page => params[:page], :per_page => 2)   end    # /listings/1   # /listings/1.json   def show   end end 

this user model:

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" 

# 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]    # /listings/1   # /listings/1.json   def show     @purchases = current_user.purchases.recent     @sales = current_user.sales.recent   end end  # show.html.erb # purchases orders <% @purchases.each |purchase_order| %>   <tr>     <td class="center"><%= purchase_order %></td>   </tr> <% end %>  # sales orders <% @sales.each |sale_order| %>   <tr>     <td class="center"><%= sale_order %></td>   </tr> <% end %> 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -