1

我是 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 %

我的图书模型如下:- class Book < ActiveRecord::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

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

4

3 回答 3

2

当然,您不能name为数字调用方法。尝试使用c.book.name.

于 2013-05-29T06:11:32.003 回答
1

您只需要执行以下操作

<%= c.book.name %>
于 2013-05-29T06:12:45.783 回答
1

更改<%= c.book_id.name %><%= c.book.name %>

于 2013-05-29T06:12:46.210 回答