gpt4 book ai didi

javascript - RoR : destroy. js.erb 未与销毁操作一起运行

转载 作者:行者123 更新时间:2023-12-02 15:41:22 25 4
gpt4 key购买 nike

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

这是相关文件,如果更多信息有用,请告诉我。

应用程序/ Controller /swipes_controller.rb

class SwipesController < ApplicationController

def new


end

app/views/swipes/destroy.js.erb

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

app/views/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>

app/views/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 %>

“app/views/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教程。

最佳答案

发生这种情况是因为 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任何地方(既不在 Controller 的 destroy 操作中,也不在 destroy.js.erb 中)此变量初始化为 nil ,其中没有任何 .size方法,这会导致您遇到错误。

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

祝你有美好的一天!

关于javascript - RoR : destroy. js.erb 未与销毁操作一起运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com