Reputation: 5009
I've got Ajax working on a create action in my Rails 3 app, so I have the proper files and everything. I'm attempting to also destroy using Ajax, but I'm having trouble. My app deletes it but I have to refresh my view for the object to be removed - it remains until I refresh. Here's what I see in rails server:
Started DELETE "/awards/6" for 127.0.0.1 at Mon Nov 28 15:12:45 -0500 2011
Processing by AwardsController#destroy as JS
Parameters: {"id"=>"6"}
Award Load (1.1ms) SELECT "awards".* FROM "awards" WHERE "awards"."id" = '6' LIMIT 1
SQL (0.4ms) BEGIN
SQL (1.5ms) SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"awards"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
AREL (0.6ms) DELETE FROM "awards" WHERE "awards"."id" = 6
SQL (1.1ms) COMMIT
Rendered text template (0.0ms)
Can anyone help me understand what's going on? Here's my code:
awards_controller.rb
:
def create
@award = Award.new(params[:award])
@award.save!
respond_to do |format|
format.html { redirect_to profile_path(@profile) }
format.js { }
end
end
def destroy
@award = Award.find(params[:id])
@award.destroy
respond_to do |format|
format.html { redirect_to profile_path(@profile) }
format.js { render :nothing => true }
end
end
The HTML in my view:
<div id="awardList">
<ul id="awardInfo">
<li>- Test<span class="delete"><a href="/awards/6" class="delete_award" data-confirm="Are you sure you want to remove this award?" data-method="delete" data-remote="true" rel="nofollow">x</a></span></li>
</ul>
</div>
<div id="newActivity">
<form></div>
...
</form>
</div>
</div><!-- end activitiesawards -->
link_to :method => :delete
:
<%= link_to 'x', award_path(award), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this award?", :class => "delete_award" %>
awards/destroy.js.erb
:
$("ul#awardInfo<%= (@award) %>").remove();
My Award.rb
model:
class Award < ActiveRecord::Base
belongs_to :profile
end
My Profile.rb
model:
class Profile < ActiveRecord::Base
has_many :awards
end
Upvotes: 2
Views: 2607
Reputation: 336
Maybe this chunk
"ul#awardInfo<%= (@award) %>"
should be
"ul#awardInfo_<%= @award.id %>"
and in view
"id="awardInfo"
id must be unique, so try
id="awardInfo_#{@award.id}"
in your view.
Upvotes: 4
Reputation: 5973
In the controller, you are calling render :nothing => true
with the js response, thus the js template is not being called.
Just left it blank ({}
) so the default template is called and you should be ok.
Upvotes: 0