0

我已经使用本教程 http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3实现了星级评分系统

Ajax 工作得很好,我在 BOOKS SHOW PAGE 上的方法也是如此

但是由于某种原因,我的帮助方法在我列出多本书的索引页面上不起作用,因此我无法在我的索引页面上调用 current_user_rating。

它只是不显示,就像在显示页面上显示整数或当前用户值一样,在索引页面上。即使它包含一个值,它也只会显示“N/A”。

我尝试了不同的东西,但似乎无法让它工作......

如果您需要我添加其他内容,请告诉我

Rails 新手请帮忙:)

帮手

module BooksHelper 

##The first part of each Methods work on Show Page but Not Index

  def rating_ballot
    if @rating = current_user.ratings.find_by_book_id(params[:id])   ****
      @rating
    else
      current_user.ratings.new
    end
  end

  def current_user_rating
    if @rating = current_user.ratings.find_by_book_id(params[:id])   ****
     @rating.value
    else
      "N/A"
    end
  end

end

意见

show.html(书籍)

<div id="book<%= @book.id %>">
   <div id="rating">
     <%= render :partial => 'ratings/rating', :locals =>{:book => @book} %>
   </div>
</div>

index.html.erb(书籍)

  <% @books.each do |book| %>
    <table id="book<%= book.id %>">
      <tbody>  
        <tr>  
          <td>
            <%= book.title %>
          </td> 
        </tr> 

        <tr>

          <td  id="rating">                   
            <%= render :partial => 'ratings/rating', :locals =>{:book => book} %>
          </td>

        </tr>
      <tbody>
    </table>
  <% end %>

_rating.html.erb(评分)

 ###This Doesn't work in my INDEX PAGE :/

 Your Rating <%= current_user_rating %> 

 <%= form_for rating_ballot, :html => { :class => 'rating_ballot' }, :remote => true do |f| %>
   <%= render 'shared/error_messages', object: f.object %>

   <%= f.label("value_1", content_tag(:span, '1'), {:class=>"rating", :id=>"1"}) %>
   <%= radio_button_tag("rating[value]", 1, current_user_rating == 1, :class => 'rating_button') %>

   <%= f.label("value_2", content_tag(:span, '2'), {:class=>"rating", :id=>"2"}) %>
   <%= radio_button_tag("rating[value]", 2, current_user_rating == 2, :class => 'rating_button') %>

   <%= f.label("value_3", content_tag(:span, '3'), {:class=>"rating", :id=>"3"}) %>
   <%= radio_button_tag("rating[value]", 3, current_user_rating == 3, :class => 'rating_button') %>

   <%= f.label("value_4", content_tag(:span, '4'), {:class=>"rating", :id=>"4"}) %>
   <%= radio_button_tag("rating[value]", 4, current_user_rating == 4, :class => 'rating_button') %>

   <%= f.label("value_5", content_tag(:span, '5'), {:class=>"rating", :id=>"5"}) %>
   <%= radio_button_tag("rating[value]", 5, current_user_rating == 5, :class => 'rating_button') %>

   <%= hidden_field_tag("issue_id", issue.id) %>
   <%= f.submit :Submit, :style => "display:none" %>

 <% end %>

模型

class User < ActiveRecord::Base
  attr_accessible :name, :email, 

  has_many :ratings, dependent: :destroy
  has_many :rated_books, :through => :ratings, :source => :books

end

class Rating < ActiveRecord::Base
  attr_accessible :book_id, :user_id, :value

  belongs_to :user
  belongs_to :book

end

class Book < ActiveRecord::Base
  attr_accessible :description, :title

  has_many  :ratings
  has_many  :raters, :through => :ratings, :source => :users

end

控制器

class BooksController < ApplicationController

  respond_to :html, :js

  def show
    @book = Book.find(params[:id])
  end

  def index 
    @books = Book.all(:include => :ratings, :order => "book_number")
  end

end
4

1 回答 1

1

当你通过时show,你有这个:

@book = Book.find(params[:id])

所以大概你有params[:id]你的助手所依赖的。但是,除了部分通过时,您为什么会期望params[:id]其他任何东西呢?您通过对.nilindexshowparams

我的建议是(几乎)永远不要params在辅助方法中访问,如果您通过辅助方法的参数传递尽可能多的信息,您将拥有更清晰、更可维护(更不用说错误更少)的代码。

你的助手应该看起来更像这样:

def rating_ballot(book_id)
  if @rating = current_user.ratings.find_by_book_id(book_id)
  #...

def current_user_rating(book_id)
  if @rating = current_user.ratings.find_by_book_id(book_id)
  #...

然后在你的部分中你可以说:

Your Rating <%= current_user_rating(book.id) %>

我还建议您不要@rating在助手内部使用,这只会用杂散的实例变量(甚至与它们的来源没有明确的联系)污染您的命名空间,而只需使用局部rating变量。

于 2014-01-02T03:21:58.253 回答