Rails帮助创建ajax星级评级

时间:2011-04-02 13:53:58

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

基于这两个答案创建一些简单的ajax评级我有点麻烦。 Rails 3 rateable model - How to create ajax rating?Rails ajax rating help how to create a helper method to display stars

我的kategori视图中出现以下错误:

NameError in Kategoris#show

Showing C:/Rails/konkurranceportalen/app/views/kategoris/_rating.html.erb where line #2 raised:

undefined local variable or method `f' for #<#<Class:0x3c77820>:0x3c724f8>

Extracted source (around line #2):

1: <%= form_tag url_for(controller: 'konkurrancers',  action: 'rate', id: "#{vind.id}"), remote: true %>
2: <%= f.text_field :ratings, :size => 1, :min => 0, :max => 5, :step => 1 %>
3: <div class="rateit" data-rateit-backingfld="#konkurrancer_ratings" data-rateit-resetable="true">
4: </div>
5: </form>

我的分类显示视图:

<h1><%= @kategori.h2.force_encoding("UTF-8") %></h1>
<div id="konkurrancer"><%= render 'konkurrencer', :remote => true %></div>
</div>

我的发生者部分:

<div id="tabel">
<table id="tabel1" border="0" bordercolor="#000000" style="background-color:#FFFFFF" width="725" cellpadding="0" cellspacing="0">
    <tr id="toptr">
      <td><%= sortable "name", "Navn" %></td>
   </tr>
<% @konkurrancer.each do |vind| %>
   <tr class="thumbnail-item" onclick="window.open('<%= vind.tracking %>')" onmouseout="this.style.background='white';" onmouseover="this.style.background='#99ff33';this.style.cursor='pointer'">
      <td width="280px" style="padding-left: 5px;"><%= truncate(vind.name.force_encoding("UTF-8"), :length => 45) %></td>

<td><div id="container"><%= render :partial => "rating", :remote => true, :locals => { :vind => vind } %></div></td>

</div>
   </tr> <% end %>
</table>
<div id="pagenavi">
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
</div>

我的评分偏:

<%= form_tag url_for(controller: 'konkurrancers',  action: 'rate', id: "#{vind.id}"), remote: true %>
<%= f.text_field :ratings, :size => 1, :min => 0, :max => 5, :step => 1 %>
<div class="rateit" data-rateit-backingfld="#konkurrancer_ratings" data-rateit-resetable="true">
</div>
</form>

我的rate.js:

$('#<%= @container %>').html('<%= escape_javascript(render(partial: 'rating', locals: { konkurrancer: @konkurrancer })) %>');

我的konkurrancers控制器:

  def rate
    @konkurrancer = Konkurrancer.find(params[:id])
    @container = "Konkurrancer"+@Konkurrancer.id.to_s

    @theme.rating_score += params[:rating].to_i
    @theme.ratings += 1
    @theme.save

    respond_to do |format|
        format.js
    end
    end
end

2 个答案:

答案 0 :(得分:2)

问题是你的部分中没有定义vind,你也没有将它作为局部变量传递。要解决此问题,您需要更改rate.js以将vind作为本地变量而不是konkurrancer

$('#<%= @container %>').html('<%= escape_javascript(render(partial: 'rating', locals: { vind: @konkurrancer })) %>');

并在你的konkurrancer部分:

<%= render 'rating', :remote => true, locals: { vind: vind } %>

然后,您可以将评分区分为id: vind.id

答案 1 :(得分:0)

对于Rails 3.1及更高版本,您可以查看letsrate gem