开发者

Rails help creating ajax star rating

I am having a bit trouble creating some simple ajax rating based on these two answers. Rails 3 rateable model - How to create ajax rating? and Rails ajax rating help how to create a helper method to display stars

I get the following error in my kategori view:

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>

My kategori show view:

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

My konkurrencer partial:

<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", :rem开发者_StackOverflowote => 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>

My ratings partial:

<%= 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>

My rate.js:

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

My konkurrancers controller:

  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


The problem is that vind is not defined in your partial and you aren't passing it as a local variable either. To fix this you'll want to change rate.js to pass vind as the local variable instead of konkurrancer:

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

and in your konkurrancer partial:

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

You can then leave your ratings partial as it is with id: vind.id.


For Rails 3.1 and higher version, you can check the letsrate gem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜