Ajaxified Star Rating System未正确更新
我使用本教程实施了星级评分系统 http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3
Ajax完美运行,直到我添加Javascript,提交表单/在给定用户进行更改后保存数据。
出于某种原因,它将遍历我的索引页面上的所有元素,并在所选对象上提交正确的整数值,但随后在其余部分上提交一堆NIL值。 我只希望它更新那个ONE对象。
( 如何在Javascript中提交相应的图书ID? )
新的铁路请帮助:)
VIEWS
index.hrml.erb(书籍)
<table id="book"> #####This is my partial for my form 'ratings/rating', :locals =>{:book => book} %>
_rating.html.erb
Avg. Rating { :class => 'rating_ballot' }, :remote => true do |f| %> "rating", :id=>"1"}) %> 'rating_button') %> "rating", :id=>"2"}) %> 'rating_button') %> "rating", :id=>"3"}) %> 'rating_button') %> "rating", :id=>"4"}) %> 'rating_button') %> "rating", :id=>"5"}) %> 'rating_button') %>
create.js.erb&update.js.erb
$('table#book td#rating').html(" 'ratings/rating', :locals => {:book => @book}) %>");
JAVASCRIPT
rating_ballot.js
####This Submits the Radio Button, but Loops through every Book on the page. $(document).ready(function() { ###Submits the form (saves data) after user makes a change. $('.rating_ballot').change(function() { $('.rating_ballot').submit(); }); });
CONTROLLER
class RatingsController < ApplicationController before_filter :current_user, only: [:create, :update] respond_to :html, :js def create @book = Book.find_by_id(params[:book_id]) @rating = Rating.create(params[:rating]) @rating.book_id = @book.id @rating.user_id = current_user.id if @rating.save respond_to do |format| format.js format.html { redirect_to :back } end end end def update @book = Book.find_by_id(params[:book_id]) @rating = current_user.ratings.find_by_book_id(@book_id) if @rating.update_attributes(params[:rating]) respond_to do |format| format.js format.html { redirect_to :back } end end end end
这是bcoz无论何时更改任何单个评级,所有评级表都会提交更改您的JS代码,如下所示
$(document).ready(function() { $('.rating_button').change(function() { $(this).parents('form:first').submit(); }); });
上面的代码也不适用于JS加载的内容。 即,您添加评级后将不会提交您的评级表,并且该表格是从create.js.erb或update.js.erb更新的
将其更改为
$(document).ready(function() { $(document).on('change', '.rating_button', function(){ $(this).parents('form:first').submit(); }); });
- 如何将包含’include’关联的记录转换为JSON
- 部分Javascript(Ruby on Rails)
- Rails复选框AJAX调用,不想渲染任何东西
- pushState with Rails ajax
- Rails – 通过ajax显示索引和显示操作的结果
- Rails 3:未定义的方法`remote_form_for’
- 使用Rails,jquery和best_in_place gem进行就地编辑
- Javascript不是在脚本中呈现而是作为html呈现
- 将Ajax POST请求的参数格式化为Rails控制器 – 用于jQuery-UI可排序列表
Interesting Posts