0
私の脳はこれを見てから揚げられています!誰が間違っているのを見ることができますか? Ajaxが正常に動作していた場合、html.erbページで部分的にレンダリングする必要はありませんが、@profileオブジェクトが部分的に読み込まれたときにデータが読み込まれない可能性があります。ここでRails - 500サーバー上のJQuery Ajax 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)
ログが何を言ってんを使用するように部分的に変更します。
もう一つは、おそらくよりエレガント、オプションでは、あなたのこれまでのレンダリングに変更するのですか? –
'@ rate_this_profile'を定義しますが、' @profile'は定義しませんので、 '@ profile'は最初に使用するときに自動的に作成され、' nil'として開始されます。だからあなたのバグがあります。私は「あまりにもローカライズされた」と締めくくっています。 –
@muistooshort私はファイルの一部しか表示していません。私が関連すると思った部分。もっと見たいと思ったら、お尋ねください。プロファイルは、残りのhtmlではnilではありません。それはそれがゼロであるという部分的なものでしかない。 – spuriosity