Rails如何根据id在索引页面中显示数据

时间:2014-01-05 18:57:44

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

我是rails的新手我正在尝试的是在我的控制器中我有两种方法

def index
 @books = Book.all
end

我的index.html看起来像

       s.no     Name        author      
        1        rails       raj                        
        2        electronics ravi                      

我想改变我的Index.html.erb的视图,如底部所示

并在

def show
    @book = Book.find(params[:id])
    book_rating = BookRating.where(:book_id =>@book.id)
    rating_size = book_rating.size
    @avg_satisfaction = book_rating.collect(&:satisfaction).sum.to_f/rating_size
    @count= book_rating.count
end 

我想要做的是根据索引页面上的书籍显示计数和avg_satisfaction,如下所示

  s.no     Name        author      satisfaction    count  
    1        rails       raj                45         1
    2        electronics ravi               50         2

请告诉我怎么做,因为我无法理解。我能够在我的show.html.erb中显示,但我想在我的index.html.erb中显示。

请帮我解决。

My model for Books
class Book < ActiveRecord::Base
    has_many :ratings, :through=> :users

    has_many :book_profiles
  has_many :profiles, :through => :book_profiles
  accepts_nested_attributes_for :book_profiles, :allow_destroy => true

  has_attached_file :logo, :styles => { :medium => "300x300>", :thumb => "100x100>" },
   :default_url => "/images/:style/missing.png"




end

mY BOOK RATINGS CONTROLLER

  class BookRating < ActiveRecord::Base
      belongs_to :user
      belongs_to :Book
    end

index.html.erb

<h1>Books List</h1>

<table id="book" border="3" layout= "fixed" width=" 100%"  >
  <colgroup>
    <col style="width: 33%" />
    <col style="width: 33%" />
    <col style="width: 33%" />

  <tr>
    <th>#</th>
    <th>Name</th>
    <th>Logo</th>
    <th>Place</th>
    <th> RATING</th>
    <th> Rank</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @books.each do |book| %>  
  <tr >
    <td><%= book.id%></td>
    <td><%= book.name %></td>
    <td><%=image_tag book.logo.url(:thumb)%></td>
    <td><%= book.place %></td>
    <td><%= book.satisfaction %></td>
    <td><%= book.rating %></td>
    <% if !current_user.blank? %>
      <td><%= link_to 'VIEW', book %></td>
    <%else%>
      <td><%= link_to 'VIEW', new_user_session_path,:class=>"login" %></td>
    <%end %>


  </tr>
<% end %>
 </colgroup>
</table>

<br />

<%= link_to 'ADD New book', new_book_path %>

1 个答案:

答案 0 :(得分:1)

我建议您在satisfaction模型中添加ratingBook个实例方法:

# app/models/book.rb
class Book < ActiveRecord::Base
  ...
  def satisfaction
    book_rating.collect(&:satisfaction).sum.to_f/rating
  end

  def rating
    book_rating.count
  end
end

然后在你看来:

# app/views/books/index.html.erb
<% @books.each do |book| %>
  <tr>
    ...
    <td><%= book.satisfaction %></td>
    <td><%= book.rating %></td>
    ...
  </tr>
<% end %>

BooksController类似于index行动的情况下,您的show行动就是:

# app/controllers/books_controller.rb
def show
    @book = Book.find(params[:id])
end

更新:要显示overall_rating,请在books模型中添加此方法:

# app/models/book.rb
def overall_rating
  ratings.sum('satisfaction + rating + view')/3
end

然后在你看来:

<% @books.each do |book| %>
  <tr>
    ...
    <td><%= book.overall_rating %></td>
    ...
  </tr>
<% end %>