ruby on rails - How can I show the average rating in index page? -


currently, average rating in show page.

but how show average rating in index besides each surf school's name? i'm having trouble because on index, use @surf_school.each |surf_schools|.

the code below code showing average rating in show page.

surf_schools_controller.rb

class surfschoolscontroller < applicationcontroller   before_action :authenticate_user!, except: [:index, :show]     before_action :surf_school_find, only: [:show, :edit, :update, :destroy]   before_action :correct_user, only: [:edit, :update, :destroy]    def index     @surf_schools = surfschool.all   end    def new     @surf_school = current_user.surf_schools.build   end    def create     @surf_school = current_user.surf_schools.build(surf_school_params)     if @surf_school.save         redirect_to surf_schools_path     else         render 'new'     end   end    def show     if @surf_school.surf_school_reviews.blank?       @average_surf_school_review = 0     else       @average_surf_school_review = @surf_school.surf_school_reviews.average(:rating).round(2)     end   end    def edit   end    def update     if @surf_school.update(surf_school_params)         redirect_to surf_schools_path     else         render 'edit'     end   end    def destroy     @surf_school.destroy     redirect_to surf_schools_path   end    private    def surf_school_params     params.require(:surf_school).permit(:name, :country, :location, :phone, :email, :url)   end    def surf_school_find     @surf_school = surfschool.friendly.find(params[:id])   end    def correct_user     unless @surf_school.user_id == current_user.id       redirect_to surf_schools_path, notice: "not authorized perform action."        false     end   end  end 

show.html.erb

<p><%= link_to "back", surf_schools_path %></p>  </br>  <h5>average rating: </h5>  <div class="average-surf_school_review-rating" data-score=<%= @average_surf_school_review %>></div>     <%= @average_surf_school_review %> (     <%= @surf_school.surf_school_reviews.count %>     <% if @surf_school.surf_school_reviews.count <= 1 %>         review     <% else %>         reviews     <% end %>     )  <p><%= link_to "add review", new_surf_school_surf_school_review_path(@surf_school) %></p>  </br>  <p><%= render @surf_school.surf_school_reviews %></p>    <script>     $('.surf_school_review-rating').raty({         readonly: true,         score: function() {             return $(this).attr('data-score');         },         path: '/assets/'     }); </script>  <script>     $('.average-surf_school_review-rating').raty({         readonly: true,         path: '/assets/',         score: function() {             return $(this).attr('data-score')         }     }); </script> 

and here's index.html.erb

<% @surf_schools.each |surf_school| %>     <h2><%= surf_school.name %></h2>     <p>country: <%= surf_school.country_name %></p>     <p>location: <%= surf_school.location %></p>     <p>contact #: <%= surf_school.phone %></p>     <p>e-mail: <%= surf_school.email %></p>     <p>         <%= link_to "visit site", surf_school.url, target: "_blank" %>         <%= link_to "read reviews", surf_school %>     </p>     <% if current_user == surf_school.user %>         <p>             <%= link_to "update", edit_surf_school_path(surf_school) %>             <%= link_to "unlist", surf_school, method: :delete, data: {confirm: "are sure?"} %>         </p>     <% end %> <% end %> 

try in index page ....

<% @surf_schools.each_with_index |surf_school, i| %>   <div class="average-surf_school_review-rating-<%= i+1 %>" data-score="<%= surf_school.surf_school_reviews.average(:rating).round(2) %>"></div> <% end %>  <%= hidden_field_tag 'surf_schools', @surf_schools.count %>  <script>         if($('#surf_schools').val() > 0){       (i = 1; <= $('#surf_schools').val(); i++) {         $('.average-surf_school_review-rating-'+i).raty({           readonly: true,           path: '/assets/',           score: function() {             return $(this).attr('data-score')           }               });       };     } </script> 

hope work you.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -