Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery/AJAX错误=>;xhr.send((options.hasContent&;options.data)| null);_Jquery_Ruby On Rails_Ajax - Fatal编程技术网

Jquery/AJAX错误=>;xhr.send((options.hasContent&;options.data)| null);

Jquery/AJAX错误=>;xhr.send((options.hasContent&;options.data)| null);,jquery,ruby-on-rails,ajax,Jquery,Ruby On Rails,Ajax,我正在实施一个明星评级系统,它属于用户、评论和产品。我一直在使用教程 我的控制台出现了一个问题,我说: xhr.send( ( options.hasContent && options.data ) || null ); 当我在我的产品/展示页面中点击一颗星星时,就会发生这种情况。页面上什么也没有发生,浏览器控制台首先给我一个500错误,然后说XHR已完成加载:“POST”: 我不认为这是一个跨域错误,因为我在本地环境中运行它。另外,我在AJAX调用中添加了dataType=

我正在实施一个明星评级系统,它属于用户、评论和产品。我一直在使用教程

我的控制台出现了一个问题,我说:

xhr.send( ( options.hasContent && options.data ) || null );
当我在我的产品/展示页面中点击一颗星星时,就会发生这种情况。页面上什么也没有发生,浏览器控制台首先给我一个500错误,然后说XHR已完成加载:“POST”:

我不认为这是一个跨域错误,因为我在本地环境中运行它。另外,我在AJAX调用中添加了dataType='JSONP',但它仍然不起作用。这是否意味着它失败了,然后成功了?我对此感到非常困惑

这是我的产品/show.html.erb页面:

 <!--Rating system -->
  <% form_id = "product_#{@product.id}_rating" %>
    <% if user_signed_in? %> <!-- To avoid throwing an exception if no user is signed in -->
      <% user_id = user_signed_in? ? current_user.id : "-1" %>
    <% else %>
      <% user_id = -1 %>
    <% end %>
    <h4>Add a rating:</h4>        
    <%= form_for @product.ratings.find_or_create_by(user_id: user_id), :html => {:id => form_id, :class => "star_rating_form"} do |f| %>
        <%= f.hidden_field :product_id, :value => @product.id %>
        <% if user_signed_in? %>
            <%= f.hidden_field :user_id, :value => current_user.id %>
        <% end %>        
        <%= f.hidden_field :score, :id => form_id + "_stars" %>
    <% end %>
    <% (1..5).each do |i| %>
      <li class="rating_star" id="<%= form_id %>_<%= i %>" data-stars="<%= i %>" data-form-id="<%= form_id %>"></li>
    <% end %>
    <br><br>
    <%= link_to "Back", products_path %> | 
    <%= link_to 'Edit', edit_product_path(@product) %>

提前感谢大家的帮助。

这可能是因为即使我在本地主机上运行此操作,您也会这样做吗?那么域、路径和端口与原点相同吗?我还添加了JSONP,但没有解决。
 <!--Rating system -->
  <% form_id = "product_#{@product.id}_rating" %>
    <% if user_signed_in? %> <!-- To avoid throwing an exception if no user is signed in -->
      <% user_id = user_signed_in? ? current_user.id : "-1" %>
    <% else %>
      <% user_id = -1 %>
    <% end %>
    <h4>Add a rating:</h4>        
    <%= form_for @product.ratings.find_or_create_by(user_id: user_id), :html => {:id => form_id, :class => "star_rating_form"} do |f| %>
        <%= f.hidden_field :product_id, :value => @product.id %>
        <% if user_signed_in? %>
            <%= f.hidden_field :user_id, :value => current_user.id %>
        <% end %>        
        <%= f.hidden_field :score, :id => form_id + "_stars" %>
    <% end %>
    <% (1..5).each do |i| %>
      <li class="rating_star" id="<%= form_id %>_<%= i %>" data-stars="<%= i %>" data-form-id="<%= form_id %>"></li>
    <% end %>
    <br><br>
    <%= link_to "Back", products_path %> | 
    <%= link_to 'Edit', edit_product_path(@product) %>
$(function() {


$('.rating_star').click(function() {
    var star = $(this);
    var form_id = star.attr("data-form-id");
    var stars = star.attr("data-stars");

    function update_stars(){
      $('.star_rating_form').each(function() {
        var form_id = $(this).attr('id');
        set_stars(form_id, $('#' + form_id + '_stars').val());
      });
    }

    function set_stars(form_id, stars) {
      for(i = 1; i <= 5; i++){
        if(i <= stars){
          $('#' + form_id + '_' + i).addClass("on");
        } else {
          $('#' + form_id + '_' + i).removeClass("on");
        }
      }
    }

    $('#' + form_id + '_stars').val(stars);

    $.ajax({
      type: "post",
      url: $('#' + form_id).attr('action') + '.json',
      data: $('#' + form_id).serialize(),
      success: function(response){
        console.log(response);
        update_stars();
        if(response["avg_rating"]){
          $('#average_rating').text(response["avg_rating"]);
          }
        }
      })
  });        
});
class RatingsController < ApplicationController
 def create
    @rating = Rating.new(params[:rating])
    @product = Product.find(params[:rating][:product_id])

    respond_to do |format|
      if @rating.save
        format.json { render :json => { :avg_rating => @product.avg_rating } }
      else
        format.json { render :json => @rating.errors, :status => :unprocessable_entity }
      end
    end
  end

  def update
    @rating = Rating.find(params[:id])
    @product = Product.find(params[:rating][:product_id])
    @comment = @rating.comment
    @rating.update_attributes(params[:rating])

    respond_to do |format|
      if @rating.save
        format.json { render :json => { :avg_rating => @product.avg_rating } }
      else
        format.json { render :json => @rating.errors, :status => :unprocessable_entity }
      end
    end
  end

  def rating_params
    params.require(:rating).permit(:score)
  end
end
class Rating < ActiveRecord::Base
  belongs_to :comment
  belongs_to :user
  belongs_to :product
end
class Product < ActiveRecord::Base
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_many :ratings
  validates :title, presence: true,
                    length: { minimum: 5, maxmimum: 140 }
  validates :price, presence: true
  validates :description, presence: true,
                  length: { minimum: 5 }

  def avg_rating
    average_rating = 0.0
    count = 0
    ratings.each do |rating| 
      average_rating += rating.score
      count += 1
    end

    if count != 0
      (average_rating / count)
    else
      count
    end
  end

end
li.rating_star {
  float:left;
  list-style: none;
  margin: 0;
  padding: 0; 
  width: 25px;
  height: 25px;
  background: asset-url('star.png', image) top left;
} 

li.rating_star.on {
  background: asset-url('star.png', image) 0 -25px;
}