2016-11-29 41 views
0

我有一個跟蹤電影租賃的Rails應用程序。我在租借表和我的客戶表之間建立了關係。每個租金都與一個客戶相關聯。Rails應用程序 - 顯示客戶名稱

在新的租賃視圖中,我有一個表格顯示所有租金及其基本信息。我想顯示的客戶名稱是每個租用的行。現在我有r.customer_id,它爲出租提供了customer_id。但我想要那個人的名字。這裏是我的 「新」 出租觀點:

<h4 class='white'>Rental Records</h4> 
<% if @movie.rentals.exists? %> 
<div class="bg_white"> 
<table class="table table-hover table-striped"> 
    <tr> 
     <th>Borrowed</th> 
     <th>Returned</th> 
     <th>Customer</th> 
     <th></th> 
     <th></th> 
    </tr> 
    <% for r in @movie.rentals.order(borrowed_on: :desc) %> 
     <% if !r.borrowed_on.nil? %> 
      <% if r.returned_on.nil? %> 
       <tr class="red"> 
        <td> 
         <%= r.borrowed_on %> 
        </td> 
        <td> 
         <%= r.returned_on %> 
        </td> 
        <td> 
         <%= r.customer_id %> 
        </td> 
        <td> 
         <%= link_to "Update", edit_movie_rental_path(@movie, r) %> 
        </td> 
        <td> 
         <%= link_to "Delete", movie_rental_path(@movie, r), method: :delete %> 
        </td> 
       </tr> 

      <% else %> 
       <tr> 
        <td> 
         <%= r.borrowed_on %> 
        </td> 
        <td> 
         <%= r.returned_on %> 
        </td> 
        <td> 
         <%= r.customer_id %> 
        </td> 
        <td> 
         <%= link_to "Update", edit_movie_rental_path(@movie, r) %> 
        </td> 
        <td> 
         <%= link_to "Delete", movie_rental_path(@movie, r), method: :delete %> 
        </td> 
       </tr> 
      <% end %> 
     <% end %> 
    <% end %> 
</table> 
</div> 
<% else %> 
<p class="white"><i>No rentals to show</i><br /><br /></p> 
<% end %> 

出租型號:

class Rental < ApplicationRecord 
has_one :movie 
has_one :customer 
end 

客戶模式:

class Customer < ApplicationRecord 
has_many :rentals 

def full_name 
    "#{self.fname} #{self.lname}" 
end 
end 

路線:

Rails.application.routes.draw do 
resources :customers 

resources :movies do 
resources :rentals 
end 
root 'movies#new' 
end 

客戶控制器:

class CustomersController < ApplicationController 
def new 
@customer = Customer.new 
@customers = Customer.all.order(lname: :asc).paginate(:page => params[:page], :per_page => 15) 
end 

def create 
@customer = Customer.new(customer_params) 
if @customer.save 
    redirect_to new_customer_path 
end 
end 

def edit 
@customer = Customer.find(params[:id]) 
end 

def update 
@customer = Customer.find(params[:id]) 
@customer.update(customer_params) 
if @customer.save 
    redirect_to new_customer_path 
end 
end 

def destroy 
@customer = Customer.find(params[:id]) 
@customer.destroy 
if @customer.destroy 
    redirect_to new_customer_path 
end 
end 

private 

def customer_params 
params.require(:customer).permit(:fname, :lname, :telephone, :email) 
end 
end 

回答

1

爲什麼不在模型租賃上使用belongs_to?

在我看來

class Rental < ApplicationRecord 
    belongs_to :movie 
    belongs_to :customer 
end 

class Customer < ApplicationRecord 
    has_many :rentals 

    def full_name 
    "#{self.fname} #{self.lname}" 
    end 
end 

,並在視圖中,您可以撥打

<%= r.customer.full_name %> 

有了上面的代碼,我認爲HAS_ONE仍然可以工作,但belongs_to的是更好

3

您可以在租賃模式做r.customer.name雖然違反迪米特法則,這樣你就可以(首選)做

delegate :name, to: :customer, prefix: true 

,然後做

r.customer_name 
相關問題