我已经让Ajax在我的Rails 3应用程序中处理创建操作,所以我有适当的文件和所有内容。我也试图使用Ajax破坏,但我遇到了麻烦。我的应用删除了它,但我必须刷新我要删除的对象的视图 - 它一直保持到刷新为止。这是我在rails服务器中看到的内容:
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)
任何人都可以帮我理解发生了什么吗?这是我的代码:
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
我的观点中的HTML:
<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();
我的Award.rb
型号:
class Award < ActiveRecord::Base
belongs_to :profile
end
我的Profile.rb
型号:
class Profile < ActiveRecord::Base
has_many :awards
end
答案 0 :(得分:4)
也许这个块
"ul#awardInfo<%= (@award) %>"
应该
"ul#awardInfo_<%= @award.id %>"
在视野中
"id="awardInfo"
id必须是唯一的,所以试试吧
id="awardInfo_#{@award.id}"
在你看来。
答案 1 :(得分:0)
在控制器中,您使用js响应调用render :nothing => true
,因此未调用js模板。
将其留空({}
),以便调用默认模板,您应该没问题。