无法在视图中显示rails 3.2 backend(postgresql)中的多个数据

时间:2013-03-12 04:22:20

标签: ruby-on-rails ruby-on-rails-3 html5 postgresql ruby-on-rails-3.2

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

我正在处理的表格是AdminBooksLibrary_LocationsUsersCategories

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上尝试过它。视图是: -

<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

任何人都可以给我一个关于如何实现这个

的建议

1 个答案:

答案 0 :(得分:0)

我认为你可能过度思考这个问题了,或者你没有读过Michael Hartl's phenomenal Rails Tutorial这是100%免费在线(并且有便宜的printkindle版本同样)。

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

/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>