【问题标题】:How to display name of a field using an id in ruby on rails 3.2如何在 ruby​​ on rails 3.2 中使用 id 显示字段名称
【发布时间】:2013-05-29 06:08:05
【问题描述】:

我是 ruby​​ on rails 的新手,正在开发一个项目。我正在处理 HTML 部分。实际上我想显示特定 Id 的名称。但是我收到一个 Fixnum 错误提示

1:Fixnum 的未定义方法“名称”

这意味着什么。

我试图连接的两个表是贷款罚款和书籍。我正在使用书籍的 id 来获取书籍的名称。

我使用的 Html 是:-

<h1 class="List">Listing Loan Fines</h1>

<table class="table table-bordered" >
  <tr>
    <th>ID</th>
    <th>Category Id</th>
    <th>Loan Duration in Days</th>
    <th>Book Name</th>
    <th>Fine Amount</th>
    <th>Fine Duration</th>
    <th>Edit</th>
    <th>Delete</th>
  </tr>

<% @loan_fines.each do |c| %>
  <tr>
    <td><%= link_to c.id, {:action => 'show', :id => c.id} %> &nbsp;</td>
    <td><%= c.category_id %>&nbsp;</td>
    <td><%= c.loan_duration %>&nbsp;</td>
    <td><%= c.book_id.name %>&nbsp;</td>  <----Trying to get book name here
    <td><%= c.fine_amount %>&nbsp;</td>
    <td><%= c.fine_duration %>&nbsp;</td>
    <td><%= link_to 'Edit', {:action => 'edit', :id => c.id} %> &nbsp;</td>
    <td><%= link_to 'Delete', {:action => 'delete', :id => c.id},
    :data => { :confirm => "Are you sure you want to delete this value?" } %></td>
  </tr>
<% end %>
</table>
<br />

<%= link_to 'New Loan fine', new_loan_fine_path %

我的书本模型如下:- 类书<:base>

    # Virtual attribute
    attr_accessor   :author_full_name

    attr_accessible :title, :abstract, :isbn, :reference_no, :category_id, :author_last_name, :author_first_name
    attr_accessible :publisher, :call_no, :book_location, :library_location_id, :author_middle_name
    attr_accessible :edition, :deleted, :total_num_copies, :cost, :entered_by, :last_updated_by, :image, :author_full_name

    # -------- Carrierwave ------------

    mount_uploader :image, ImageUploader

    # ------- ASSOCIATIONS --------

    has_many :book_holds
    has_many :book_transactions
    has_many :loan_fines

    belongs_to :admin, :foreign_key => 'last_updated_by'
    belongs_to :admin, :foreign_key => 'entered_by'

    belongs_to :library_location
    belongs_to :category

    # -------- SCOPES ----------

    default_scope :order => 'books.id DESC'

    scope :not_deleted, where("books.deleted = 0")

    default_scope not_deleted

    # ------ VALIDATIONS ------

    validates :isbn,           :allow_blank => true, :uniqueness => { :case_sensitive => false } 
    validates :reference_no,   :allow_blank => true, :uniqueness => { :case_sensitive => false }    
    validates :call_no,        :allow_blank => true, :uniqueness => { :case_sensitive => false }    


    #----------------------------------------------------------------------------  
    #  author_full_name - Returns Full Name of author
    #----------------------------------------------------------------------------
    def author_full_name
      "#{author_last_name}, #{author_first_name} #{author_middle_name}"
    end

end

我的贷款罚款模型如下:-

class LoanFine < ActiveRecord::Base

    attr_accessible :book_id, :category_id, :loan_duration, :fine_amount, :fine_duration

    # ------- ASSOCIATIONS -----------

    belongs_to :book

    belongs_to :category

    # ------ VALIDATIONS ------

# validate :book_or_category


# if [self.book_id, self.category_id].reject(&:blank?).size == 0

# if self.book_id.blank? && self.category_id.blank?

#end
end

有人可以帮我解决这个问题,因为我是新手

【问题讨论】:

    标签: html ruby-on-rails ruby ruby-on-rails-3


    【解决方案1】:

    当然你不能为一个数字调用方法name。尝试使用c.book.name

    【讨论】:

    • 我得到未定义的方法名称 nil:Nil 类
    • 我得到了书的未定义方法名称
    • 如果@loan_fine 没有书,这将显示一个空字段。您也可以在一个块中执行此操作: 这笔贷款没有书罚款
    • @magnum2002 我试过了,但还是报同样的错误
    【解决方案2】:

    你只需要做以下事情

    <%= c.book.name %>
    

    【讨论】:

      【解决方案3】:

      &lt;%= c.book_id.name %&gt; 更改为&lt;%= c.book.name %&gt;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-08
        • 1970-01-01
        相关资源
        最近更新 更多