0

I am newbie to rails what I am trying was in my controller I have two methods

there are

def index
 @books = Book.all
end

And My index.html looks like

       s.no     Name        author      
        1        rails       raj                        
        2        electronics ravi                      

And I would Like to change the view of my Index.html.erb as shown in the bottom

and in

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 

What I wanted to do is display the count and avg_satisfaction according to books on index page as follows

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

Please tell me how to do this, as I am unable to understand it. I am able to display in my show.html.erb but I want to display in my index.html.erb.

Please help me solve it.

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 %>
5
  • You have a space in Book. all. Should be Book.all. Commented Jan 5, 2014 at 19:01
  • You already have @books = Book.all, now you just need to add to your views/books/index.htm.erb to display that info. What does your current index.htm.erb view look like? Commented Jan 5, 2014 at 19:06
  • Are you sure it's not the space in Book. all that is preventing your index.html.erb from showing all the books? If you generated that code with rails generate.... the table view should already be in index.html.erb. Commented Jan 5, 2014 at 19:08
  • Hi every thing is working fine with my index.html but I want to add the extra two columns there are satisfaction and count Commented Jan 5, 2014 at 19:17
  • Can you show us what ìndex.html.erb contains right now? The code, not the rendered HTML. Commented Jan 5, 2014 at 19:28

1 Answer 1

1

I'd suggest you add satisfaction and rating instance methods in your Bookmodel:

# 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

Then in your view:

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

And in your BooksController similar to your index action, your show action would just be:

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

Update: To display overall_rating, add this method in your books model:

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

Then in your view:

<% @books.each do |book| %>
  <tr>
    ...
    <td><%= book.overall_rating %></td>
    ...
  </tr>
<% end %>
Sign up to request clarification or add additional context in comments.

9 Comments

that is fine but I have a problem is I have more parameters how can I calculate it. I have parameters like (s1+s2+s3+s4)/rating. How do I get it.
@user3144005, I'm not sure I understand your parameters (s1+s2+s3+s4)/rating. What are s1, s2...?
There are the different parameter asked to user to calulate over all rating of a book. Like satisfaction, quality, rating for author and so on.and I would like calculate the over all rating.
Don't you want to keep the ratings for different attributes separate? Could you post your models and views in your question?
No I dont want individual rating for different attributes I want over all ratings. sure I will post it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.