0

我目前正在为图书馆管理系统做一个项目。我在项目中有五个表。它们都是相互关联的,即它们相互引用。

我正在处理的表格是Admin, Books, Library_Locations,UsersCategories

Books is referencing user,categories and admin .User is referencing library_location

我已经为它们创建了模型和控制器。我想知道我是否可以创建一个视图,这样如果我点击 book_id,我可以获得用户、类别和管理员的详细信息。

我的书表是:-

id serial NOT NULL,
  title character varying(255) NOT NULL,
  abstract character varying(255),
  isbn character varying(255),
  call_no character varying(255),
  reference_no character varying(255),
  category_id integer,
  library_location_id integer,
  author_first_name character varying(255),
  author_middle_name character varying(255),
  author_last_name character varying(255),
  image character varying(255),
  publisher character varying(255),
  edition character varying(255),
  book_location character varying(255),
  deleted integer NOT NULL DEFAULT 0,
  total_num_copies integer,
  cost numeric(8,2) DEFAULT 0.0,
  entered_by integer,
  last_updated_by integer,
  created_at timestamp without time zone NOT NULL,
  updated_at timestamp without time zone NOT NULL,
  CONSTRAINT books_pkey PRIMARY KEY (id)

我已经在 lov_names 和 values.The 上试过了。观点是:-

<h3>lov Name</h3><br>
<div class="span3">
<table class="table table-striped table-bordered">
<tr>    
<td>Lov Names-Id: </td><td><%= params[:id] %></td>
<tr><td>Lov Names-Name:</td><td> <%= LovName.where('id = ?', params[:id]).pluck(:name)[0] %></td></tr>
<tr><td>Lov Values-Description:</td><td> <%= LovName.where('id = ?', params[:id]).pluck(:description)[0] %></td></tr>
</tr>


</table>
</div>
</br>

   <table class="table table-hover table-bordered">

 <tr><th>lov_value id.</td><th>lov_value Name</th><th>lov_value</th><th>Sequence</th><th>Edit</th><th>Delete</th></tr>
<% @lov_value.each do |p| %>
<tr>
<td><%= p.id %> <br></td>
<td><%= p.lov_name_id %> <br></td>
<td><%= p.lov_value %> <br></td>
<td><%= p.sequence %> <br></td>

<td><%= link_to 'Edit', {:action => 'lov_value_edit', :id => p.id} %> &nbsp;</td>
<td><%= link_to 'Delete', {:action => 'lov_value_delete', :id => p.id},
    :data => { :confirm => "Are you sure you want to delete this lov_value?" } %></td>
    </tr>
</tr>
<% end %>
</table>
<br/>

<p class="btn btn-primary "><%= link_to 'Back', {:action => 'index'} %></p>
<p class="btn btn-primary "><%= link_to 'Add New lov_values', {:action => 'add_values'} %></p>

我的 Books.rb 片段:-

has_many :book_holds
has_many :book_transactions

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

belongs_to :library_location
belongs_to :category
    belongs_to :user

谁能给我一个关于如何实现这个的建议

4

1 回答 1

0

我认为您可能是在想这个问题,或者您可能没有阅读Michael Hartl 的惊人的 Rails 教程,该教程是 100% 在线免费的(并且还有廉价的印刷版kindle版)。

访问您需要的不同值应该发生在控制器类中,并且显示数据应该发生在视图中。所以你的控制器需要看起来像:

/app/controllers/books_controller.rb

class BooksController
  def index
   @books = Book.all
  end

  def show
   @book = Book.find(params[:id])
   @category = @book.category
   @lastUpdatedBy = @book.last_updated_by
   @enteredBy = @book.entered_by
   @user = @book.user
  end

  # Any other methods (edit, destroy, create, new, etc.) go here
end

现在我们定义视图:

/app/views/books/show.html.erb

<h1><%= @book.title %><h1>
<h2>By <%= @book.author %></h2>
<h3>Category: <%= @category.name %></h3>

<p> Current user: <%= @user.name %> </p>
<p> Last updated by: <%= @lastUpdatedBy.name %> </p>
<p> Entered by: <%= @enteredBy.name %> </p>
于 2013-03-12T05:11:43.967 回答