2012-01-12 64 views
1

在我的Rails 3应用程序中,我允许用户向他们的配置文件添加奖励。使用Rails UJS我得到了create与ajax一起工作。但是,我不能让我的生活得到destroy工作。看看我的服务器日志,它看起来像它的工作,但@award不会被删除,除非我刷新我的个人资料页面。使用ajax(destroy.js.erb)不能正常工作的JavaScript删除对象

我使用的代码来自于我在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(); 

我的形式删除奖,这下一个显示为 “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>的用途。

回答

3

destroy行动把format.js { render :nothing => true }据推测告诉滑轨连接到使您的destroy.js.erb

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

更新

从上面的代码,它看起来像有什么在可用于选择上为remove呼叫实际奖HTML。

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

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

然后,除去奖项,选择其li元素,并删除如下:

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

它看起来对我来说,唯一剩下的问题是,jQuery的呼叫不是选择正确的元素或任何元素来调用remove。给li一个唯一的ID,然后选择要删除的ID 应该使destroy操作正确呈现。

+0

不幸的是取出'render:nothing => true'并没有办法。 – tvalent2 2012-02-02 13:14:54

+0

这样做是否会改变日志中的“渲染文本模板”行?当你执行'render:nothing => true'时,Rails渲染一个空的文本模板。只是做'format.js'应该改变了Rails的渲染方式,如果可以的话,我可以提供更多的帮助,如果我看到现在正在渲染的东西。 – Ryan 2012-02-02 22:37:45

+0

我更新了日志显示的问题。现在至少我的'destroy.js.erb'正在渲染。所以它必须是该文件中的内容? – tvalent2 2012-02-03 12:58:15