Хорошо, мой мозг зажарен, глядя на это!Кто-нибудь может увидеть, что не так?Одна возможность состоит в том, что объект @profile не заполняется при загрузке партиала, хотя на самом деле мне не нужно отображать партиал на странице html.erb, если Ajax работал правильно!
Вот так:
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
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)