Rails应用程序 - 显示客户名称

时间:2016-11-29 23:30:31

标签: ruby-on-rails

我有一个跟踪电影租赁的rails应用。我在Rentals表和Customers表之间建立了关系。每个租赁都与一个客户相关联。

在租借的新视图中,我有一张表格,显示所有租借及其基本信息。我想显示客户名称是每个租赁的行。现在我有r.customer_id,它将customer_id拉出租金。但我想要那个人的名字。以下是我对" new"的看法出租:

<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

2 个答案:

答案 0 :(得分:3)

您可以执行r.customer.name虽然违反了demeter规则,但您可以(首选)执行

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

在租赁模型中然后再做

r.customer_name

答案 1 :(得分: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更好