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

时间:2022-06-12 03:50:16

In my Rails 3 app I am allowing users to add awards to their profiles. Using Rails UJS I get the create working with ajax. However, I can't for the life of me get destroy to work. Looking inside my server log, it looks like it's working, but the @award isn't removed unless I refresh my profile page.

在我的Rails 3应用中,我允许用户将奖励添加到他们的个人资料中。使用Rails UJS,我可以使用ajax创建工作。但是,我不能为了我的生命而毁掉我的工作。查看我的服务器日志,它看起来运行良好,但是@award不会被删除,除非我刷新我的个人资料页面。

The code I used is from a tutorial I was following in Beginning Rails 3. If anyone can help me figure out what's going on wrong in my code I'd appreciate it.

我所使用的代码来自于我在Rails 3开头所遵循的教程。如果有人能帮我弄明白我的代码出了什么问题,我会很感激的。

My awards_controller.rb:

我的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

My create.js.erb:

我的create.js.erb:

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

My destroy.js.erb:

我的destroy.js.erb:

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

My form to delete the award, which appears as an "x" next to the award if the award belongs to/was created by the current_user:

我要删除该奖项的表单,如果该奖项属于/是由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>

In my server log when I click "delete" (but before refreshing the page):

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

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)

UPDATE: If I remove render :nothing => true from my destroy action, it doesn't actually remove the award. My log shows the following:

更新:如果我从我的销毁行为中删除渲染:nothing =>为真,它实际上不会删除奖励。我的日志显示如下:

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)

UPDATE 2: So a weird thing happens if I go to check the HTML:

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

  • If I view source, I don't actually see any of the awards.
  • 如果我查看源代码,我实际上没有看到任何奖项。
  • If I inspect the element, I see the following: <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>
  • 如果我检查这个元素,我看到以下内容:
  • -&检验

The <li> is the actual award created. I have an award.rb model that belongs_to :profile. If the profile awards belong to my profile, I see a delete option, which is what the <span> is for.

  • 是实际创建的奖励。我有一个奖。belongs_to:profile的rb模型。如果profile奖属于我的个人资料,我就会看到一个删除选项,这就是是用来做什么的。

  • 是实际创建的奖励。我有一个奖.belongs_to:概要的rb模型。如果形象奖属于我的个人资料,我就会看到一个删除选项,这就是是用来做什么的。
  • 1 个解决方案

    #1


    4  

    Putting format.js { render :nothing => true } in your destroy action is presumably telling Rails to not render your destroy.js.erb.

    把格式。在您的销毁操作中,没有任何东西=> true}可能是告诉Rails不要渲染您的驱逐舰。

    Using the same format.js { } as used in the create action should direct Rails to render destroy.js.erb and subsequently execute the desired action.

    使用相同的格式。在创建操作中使用的js{}应该引导Rails呈现驱逐舰.js。然后执行所需的操作。

    Update:

    更新:

    From the code above, it looks like there's nothing in the actual award HTML that can be used to select on for the remove call.

    从上面的代码中可以看出,在实际的award HTML中,似乎没有什么可以用于为remove调用选择on。

    I'd recommend making sure the li elements include ID attributes with the award ID in them. Something like:

    我建议确保li元素包含ID属性,其中包含ID。喜欢的东西:

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

    Then, to remove the award, select its li element and remove as follows:

    然后,要删除该奖项,请选择其li元素,并删除如下内容:

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

    It looks to me like the only remaining issue is that the jQuery call isn't selecting the right element, or any element at all, to call remove on. Giving the li a unique ID and then selecting that ID for removal should make the destroy action render properly.

    在我看来,惟一剩下的问题是jQuery调用没有选择要调用remove的正确元素或任何元素。给li一个唯一的ID,然后选择这个ID进行删除,这将使销毁操作呈现正确。

    #1


    4  

    Putting format.js { render :nothing => true } in your destroy action is presumably telling Rails to not render your destroy.js.erb.

    把格式。在您的销毁操作中,没有任何东西=> true}可能是告诉Rails不要渲染您的驱逐舰。

    Using the same format.js { } as used in the create action should direct Rails to render destroy.js.erb and subsequently execute the desired action.

    使用相同的格式。在创建操作中使用的js{}应该引导Rails呈现驱逐舰.js。然后执行所需的操作。

    Update:

    更新:

    From the code above, it looks like there's nothing in the actual award HTML that can be used to select on for the remove call.

    从上面的代码中可以看出,在实际的award HTML中,似乎没有什么可以用于为remove调用选择on。

    I'd recommend making sure the li elements include ID attributes with the award ID in them. Something like:

    我建议确保li元素包含ID属性,其中包含ID。喜欢的东西:

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

    Then, to remove the award, select its li element and remove as follows:

    然后,要删除该奖项,请选择其li元素,并删除如下内容:

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

    It looks to me like the only remaining issue is that the jQuery call isn't selecting the right element, or any element at all, to call remove on. Giving the li a unique ID and then selecting that ID for removal should make the destroy action render properly.

    在我看来,惟一剩下的问题是jQuery调用没有选择要调用remove的正确元素或任何元素。给li一个唯一的ID,然后选择这个ID进行删除,这将使销毁操作呈现正确。