如何在rails 3.2上的ruby中使用id显示字段的名称

时间:2013-05-29 06:08:05

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

我是ruby on rails的新手,正在开发一个项目。我正在研究HTML部分。实际上我想显示特定Id的名称。但是我得到了一个Fixnum错误说

1:Fixnum

的未定义方法'name'

这意味着什么。

我想连接的两张桌子是贷款罚款和书籍。我使用书的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&lt;的ActiveRecord ::基

    # 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

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

3 个答案:

答案 0 :(得分:2)

当然,您无法为某个数字调用方法name。尝试使用c.book.name

答案 1 :(得分:1)

您只需要执行以下操作

<%= c.book.name %>

答案 2 :(得分:1)

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