0
好吧,我的大腦從看着這個炸!任何人都可以看到有什麼不對? 一種可能性是@profile對象沒有在部分加載時被填充,儘管如果Ajax工作正常,我實際上不需要在html.erb頁面上呈現部分!導軌 - 500從JQuery的阿賈克斯POST(也GET)服務器
這裏,我們去:
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)
是什麼日誌說? – 2011-12-18 22:12:22
您可以定義'@ rate_this_profile'而不是'@profile',因此'@ profile'在首次使用時自動創建,並以'nil'開始。所以有你的bug,我正在投票結束「太本地化」。 – 2011-12-18 22:40:06
@ muistooshort我只顯示了部分文件。我認爲相關的部分。如果您想了解更多信息,請詢問。該配置文件不爲零的其餘部分的HTML。只有部分它是零。 – spuriosity 2011-12-18 22:51:23