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

Ruby on rails Rails应用程序-显示客户名称,ruby-on-rails,Ruby On Rails,我有一个rails应用程序跟踪电影租赁。我在我的Rentals表和Customers表之间建立了关系。每个租赁与一个客户关联 在rentals的新视图中,我有一个表,显示所有租金及其基本信息。我想显示客户名称,即每次租赁的行。现在我有r.customer\u id,它为租金提取客户id。但我想知道那个人的名字。以下是我对“新”租赁的看法: <h4 class='white'>Rental Records</h4> <% if @movie.rentals.exist

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

在rentals的新视图中,我有一个表,显示所有租金及其基本信息。我想显示客户名称,即每次租赁的行。现在我有r.customer\u id,它为租金提取客户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 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
class CustomerControllerparams[:page],:per_page=>15)
结束
def创建
@客户=客户。新(客户参数)
如果@customer.save
重定向到新客户路径
结束
结束
定义编辑
@customer=customer.find(参数[:id])
结束
def更新
@customer=customer.find(参数[:id])
@customer.update(客户参数)
如果@customer.save
重定向到新客户路径
结束
结束
def销毁
@customer=customer.find(参数[:id])
@客户,销毁
如果@customer.destroy
重定向到新客户路径
结束
结束
私有的
def客户参数
参数要求(:客户).permit(:fname,:lname,:电话,:电子邮件)
结束
结束

你可以做
r.customer.name
尽管这违反了德米特的法律,所以你可以(首选)做

在租车模式下再做

r.customer_name

你可以做
r.customer.name
,尽管这违反了德米特法,所以你可以(首选)做

在租车模式下再做

r.customer_name

为什么不在模型租赁上使用属于您的

在我看来

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
class-rent
在视图中,您可以调用

<%= r.customer.full_name %>


有了上面的代码,我认为有一个仍然有效,但是属于更好

为什么不在模型租赁上使用属于呢

在我看来

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
class-rent
在视图中,您可以调用

<%= r.customer.full_name %>

有了上面的代码,我认为有一个仍然有效,但属于更好

<%= r.customer.full_name %>