RoR:destroy.js.erb没有与destroy action

时间:2015-09-12 19:35:20

标签: javascript ruby-on-rails

我的问题是我希望调用我的滑动控制器的破坏操作(它确实有效),然后它会调用文件app / views / swipes / destroy.js.erb中的javascript。想法是,在调用此文件时,它将刷新我的my-deck div并提供更新的网页。不幸的是,虽然我的销毁操作正在运行,但它似乎没有正确调用js,我必须刷新网页才能看到该项目已被删除。

以下是相关文件,如果有更多信息可用,请与我们联系。

应用程序/控制器/ swipes_controller.rb

class SwipesController < ApplicationController

  def new


end

应用程序/视图/刷卡/ destroy.js.erb

$(".my-deck").alert("Foo");

应用程序/视图/ my_deck / show.html.erb

<p> Welcome to my deck! </p>
<p> When this is working your right swiped events should appear beautifully below! </p>

<div class="my-deck">

</div>

应用程序/视图/ my_deck / _my_deck.html.erb

    <p class="text-center">
  There are no items in your shopping cart.  Please <%= link_to "go back", home_path %> and add some items to your cart.
    </p>
 <% end %>

“应用/视图/ my_deck / _card_row.html.erb”

<div class="well">

  <div class="row">
    <div class="col-xs-8">
      <h4><%= event_id %></h4>
    </div>
    <div class="col-xs-4">



        <div class="row">
          <div class="col-xs-4">

      </div>
      <div class="col-xs-8 text-right">
            <div class="btn-group">
              <%= link_to "Delete", myCard, { data: { confirm: "Are you     sure you wish to delete the product '#{myCard.event_id}' from your cart?"}, method: :delete, remote: true, class: "btn btn-danger" } %>
            </div>
          </div>
        </div>
      <% end %>
    </div>

  </div>
</div>

当我点击删除时,继承我的rails服务器的输出:

  Swipe Load (0.8ms)  SELECT  "swipes".* FROM "swipes" WHERE "swipes"."id" = $1 LIMIT 1  [["id", 48]]
   (0.1ms)  BEGIN

   (1.6ms)  COMMIT
  Rendered my_deck/_my_deck.html.erb (74.5ms)
  Rendered swipes/destroy.js.erb (79.0ms)
Completed 500 Internal Server Error in 91ms (ActiveRecord: 3.0ms)

ActionView::Template::Error (undefined method `size' for nil:NilClass)
  app/views/my_deck/_my_deck.html.erb:1:in `_app_views_my_deck__my_deck_html_erb___162867021113944140_70146212264540'
  app/views/swipes/destroy.js.erb:1:in     `_app_views_swipes_destroy_js_erb__359810691289434442_70146212224340'


  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.6ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.7ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (8.9ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.6ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.3ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (14.6ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.3ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.4ms)
  Rendered /home/tomos/.rvm/gems/ruby-2.2.3/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (30.9ms)

如果有人可以帮我解决为什么app / views / swipes / destroy.js.erb没有运行那就太好了。我正在关注this教程。

2 个答案:

答案 0 :(得分:2)

这是因为destroy.js.erb

$(".my-deck").alert("Foo");
$(".my-deck").html("<%= escape_javascript(render 'my_deck/my_deck') %>");

渲染app/views/my_deck/_my_deck.html.erb,按顺序执行此行:

<% if @myDeck.size == 0 %>

由于您未在任何地方定义@myDeck(在控制器的destroy操作中,也未在destroy.js.erb中),此变量初始化为nil,而不是.size任何导致错误的@myDeck方法。

要修复错误,您需要正确初始化$(".my-deck").html("<%= escape_javascript(render 'my_deck/my_deck') %>"); var或更改模板,以便它不再访问未初始化的变量(例如,删除行TransferHandler)。

度过美好的一天!

答案 1 :(得分:0)

我在测试一些ajax事件时遇到了类似的问题,因为我的操作中缺少某些东西:

这是我在我的销毁行动中添加的内容:

respond_to do |format|
    format.js
end

尝试将您的销毁更改为:

def destroy

    @swipe = Swipe.find(params[:id])

    if @swipe.destroy
        redirect_to myDeck_path
    end

    respond_to do |format|
       format.js
    end

end

我希望它适合你。