【问题标题】:Rails - 500 from Server on JQuery Ajax POST (also on GET)Rails - 500 来自 JQuery Ajax POST 上的服务器(也在 GET 上)
【发布时间】:2011-12-18 22:03:22
【问题描述】:

好吧,看到这个我的脑子都炸了!任何人都可以看到有什么问题吗? 一种可能性是 @profile 对象在加载部分时没有被填充,但如果 Ajax 工作正常,我实际上不需要在 html.erb 页面上呈现部分!

我们开始吧:

html.erb:

<div id="stars">
<%= render :partial => 'stars' %>
</div>

星星部分:

<p>
  <b><strong>Stars! </strong></b>
    <%= @profile.stars %>
    <input type="submit" name="one" id="one" value="+1" message="<%= @profile.id %>">
</p>

javascript:

$(function() {
  $("#one").click(function() {
$.ajax({
    type: "POST",
    url: "/rate",
    dataType: "html",
    data: "prof="+this.getAttribute("message"),
    success: function(data){
        $("#stars").html(data);
        }
    });
  })
});

和控制器:

  def rate
@rate_this_profile = Profile.find_by_id(params[:prof])
if @rate_this_profile
  @rate_this_profile.stars += 1

  respond_to do |format|           --Line 177
  if @rate_this_profile.save

    render :partial => "stars"     --Line 180

    flash[:success] = "Rated!"
  else
    flash[:success] = "Failed"
  end
end
end

结束

日志:

Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2011-12-18 22:01:51 +0000
Served asset /application.js - 304 Not Modified (3ms)


Started POST "/rate" for 127.0.0.1 at 2011-12-18 22:13:23 +0000
  Processing by ProfilesController#rate as HTML
   Parameters: {"prof"=>"3"}
   ←[1m←[35mProfile Load (0.0ms)←[0m  SELECT "profiles".* FROM "profiles" WHERE  "profiles"."id" = 3 LIMIT 1
  ←[1m←[36m (1.0ms)←[0m  ←[1mUPDATE "profiles" SET "stars" = 21, "updated_at" = '2011-12-18 22:13:23.493276' WHERE "profiles"."id" = 3←[0m
Rendered profiles/_stars.html.erb (7.0ms)
Completed 500 Internal Server Error in 93ms

ActionView::Template::Error (undefined method `stars' for nil:NilClass):
1: <p>

2:   <b><strong>Stars! </strong></b>

3:          <%= @profile.stars %>

4:          <input type="submit" name="one" id="one" value="+1" message="<%= @profile.id %>">

5: </p>
  app/views/profiles/_stars.html.erb:3:in      `_app_views_profiles__stars_html_erb__1034491404_54471564'
  app/controllers/profiles_controller.rb:180:in `block in rate'
  app/controllers/profiles_controller.rb:177:in `rate'

Rendered c:/Ruby/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered c:/Ruby/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered c:/Ruby/RailsInstaller/Ruby1.9.2/lib/ruby/gems/1.9.1/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (37.0ms)

【问题讨论】:

  • 您定义了@rate_this_profile,但没有定义@profile,因此@profile 在首次使用时自动创建并以nil 开头。所以有你的错误,我投票关闭“过于本地化”。
  • @muistooshort 我只展示了部分文件。我认为相关的部分。如果您想看更多,请询问。对于 html 的其余部分,配置文件不是 nil。它只是部分为零。
  • @spuriosity 您应该显示初始化@profile 的部分,因为Ajax post 方法没有,这就是在渲染部分之前运行的内容。其中@profilenil。当你提出一个新的请求时,是什么让你认为 *previous@ 请求的 @profile 仍然有效?
  • 为什么在调用rate 时会定义@profile
  • 你能不能试试把@profile.stars改成@profile.try(:stars),看看是不是还是报错。

标签: javascript ruby-on-rails ruby ajax post


【解决方案1】:

无论格式如何,您的控制器都在渲染他的部分星星。如果你在 rate 方法中设置@profile,它会修复它吗?

另一个可能更优雅的选项是将您的渲染更改为:

render :partial => 'stars', :locals => { :profile => whatever_you_want_here }

并将您的部分更改为使用“个人资料”而不是“@profile”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 2018-02-04
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 2016-04-28
    相关资源
    最近更新 更多