使用ajax(destroy.js.erb)删除对象的JavaScript不起作用

时间:2012-01-12 02:37:45

标签: javascript ajax ruby-on-rails-3

在我的Rails 3应用程序中,我允许用户为其个人资料添加奖励。使用Rails UJS我得到create使用ajax。但是,我不能为我的生活destroy工作。查看我的服务器日志,看起来它正在工作,但除非我刷新我的个人资料页面,否则不会删除@award

我使用的代码来自我在Beginning Rails 3中关注的教程。如果有人能帮我弄清楚我的代码中出错了什么,我会很感激。

我的awards_controller.rb

def create
  @award = Award.new(params[:award])
  if @award.save!
    respond_to do |format|
      #format.html { redirect_to profile_path(@profile) }
      format.js { }
    end
  else
    respond_to do |format|
      #format.html { redirect_to profile_path(@profile)}
      format.js { render 'fail_create_award.js.erb' }
    end
  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

我的create.js.erb

$('ul#awardInfo').append("<%= escape_javascript(render(@award)) %>");
$('#new_award')[0].reset();

我的destroy.js.erb

$("ul#awardInfo<%= (@award) %>").remove();

我的表单删除奖励,如果奖项属于/由current_user创建,则奖励旁边显示为“x”:

<li>-&nbsp;<%= award.body %><% if @profile = current_user.profile %><span class="delete"><%= link_to 'x', award_path(award), :method => :delete, :remote => true, :confirm => "Are you sure you want to remove this award?", :class => "delete_award" %></span><% end %></li>

在我的服务器日志中单击“删除”(但在刷新页面之前):

Started DELETE "/awards/8" for 127.0.0.1 at 2012-01-11 21:34:58 -0500
  Processing by AwardsController#destroy as JS
  Parameters: {"id"=>"8"}
  Award Load (1.0ms)  SELECT "awards".* FROM "awards" WHERE "awards"."id" = '8' LIMIT 1
  SQL (0.2ms)  BEGIN
  SQL (1.4ms)   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" = 8
  SQL (2.3ms)  COMMIT
Rendered text template (0.0ms)
Completed 200 OK in 31ms (Views: 1.3ms | ActiveRecord: 5.4ms)

更新:如果我从销毁操作中移除render :nothing => true,则实际上并未删除奖励。我的日志显示以下内容:

Started DELETE "/awards/3" for 127.0.0.1 at 2012-02-03 07:54:34 -0500
  Processing by AwardsController#destroy as JS
  Parameters: {"id"=>"3"}
  Award Load (238.1ms)  SELECT "awards".* FROM "awards" WHERE "awards"."id" = '3' LIMIT 1
  SQL (0.4ms)  BEGIN
  SQL (1.6ms)   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 (97.3ms)  DELETE FROM "awards" WHERE "awards"."id" = 3
  SQL (45.8ms)  COMMIT
Rendered awards/destroy.js.erb (1.1ms)
Completed 200 OK in 954ms (Views: 531.9ms | ActiveRecord: 383.2ms)

更新2:如果我去检查HTML,会发生奇怪的事情:

  • 如果我查看来源,我实际上看不到任何奖项。
  • 如果我检查元素,我会看到以下内容: <li>-&nbsp;test<span class="delete"><a href="/awards/4" 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>

<li>是创建的实际奖励。我有一个award.rb模型belongs_to :profile。如果个人资料奖励属于我的个人资料,我会看到一个删除选项,这是<span>的用途。

1 个答案:

答案 0 :(得分:4)

format.js { render :nothing => true }放入destroy行动可能会告诉Rails 渲染 destroy.js.erb

使用format.js { }操作中使用的相同create应该指示Rails呈现 destroy.js.erb ,然后执行所需的操作。

<强>更新

从上面的代码中可以看出,实际的奖励HTML中没有任何内容可用于选择remove来电。

我建议确保li元素包含带有奖励ID的ID属性。类似的东西:

<li id="award-<%= @award.id %>">

然后,要删除奖励,请选择其li元素,然后按以下步骤删除:

$('li#award-<%= @award.id %>').remove();

在我看来,唯一剩下的问题是jQuery调用没有选择正确的元素或任何元素来调用remove on。为li提供一个唯一ID,然后选择要删除的ID 应该使销毁操作正确呈现。