2014-01-05 44 views
0

我是新手到軌什麼,我想在我的控制,我有兩個方法的Rails如何在索引頁顯示的數據基於ID

def index 
@books = Book.all 
end 

和我的指標。 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 

我的書額定值控制器

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 %> 
+0

您在'書中有一個空間。 all'。應該是'Book.all'。 – Beartech

+0

你已經擁有'@books = Book.all',現在你只需要添加到你的'views/books/index.htm.erb'來顯示這些信息。你目前的index.htm.erb視圖是什麼樣的? – Beartech

+0

你確定這不是'書中的空間。所有'是否阻止你的'index.html.erb'顯示所有的書?如果您使用'rails generate ....'生成了該代碼,那麼表視圖應該已經存在於'index.html.erb'中。 – Beartech

回答

1

我建議你在Book模型添加satisfactionrating實例方法:

# 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 %> 
+0

這很好,但我有一個問題是我有更多的參數,我怎麼才能算出它。我有(s1 + s2 + s3 + s4)/ rating等參數。我如何得到它。 – user3144005

+0

@ user3144005,我不確定我是否理解你的參數'(s1 + s2 + s3 + s4)/ rating'。什麼是's1','s2' ...? – vee

+0

有不同的參數要求用戶在一本書的所有等級上進行計算。就像滿意度,質量,作者評分等等。我想計算一下所有的評價。 – user3144005

相關問題