2

我正在为我的控制器使用标准脚手架:

# DELETE /addresses/1
# DELETE /addresses/1.json
def destroy
  @address = Address.find(params[:id])
  @address.destroy

  respond_to do |format|
    format.html { redirect_to addresses_url }
    format.json { head :no_content }
  end
end

我的应用程序布局文件:

<!DOCTYPE html>
<html>
<head>
    <title>DemoRailsApp</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
    <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>

    <%= csrf_meta_tags %>
</head>
<body>
  <div data-role="page" data-theme="a" data-fullscreen="true">
    <%= yield %>
  </div>

index.html.erb

<div data-role='header'>
<h1>Addreses</h1>
<%= link_to "Add" , new_address_path, "data-icon"=> "plus" , "class" => "ui-btn-right" %>
</div>

<div class="content" data-role="content">
<ul data-role="listview">
    <% @addresses.each do |address| %> 
    <li>
        <%= link_to address.address1, edit_address_path(address) %>
        <%= link_to 'Destroy', address, confirm: 'Are you sure?', method: :delete, "data-icon" => "delete" %>
    </li>
    <% end %>
</ul>
</div>
</body>
</html>

当我单击删除链接时,将显示显示视图,而不是确认或删除。很困惑。

Edit2: 我将 <%= javascript_include_tag 'application' %> 添加到了我的 application.html.erb 文件中,记录已被删除,但现在加载页面时出现错误,以及来自服务器的以下内容:

Started DELETE "/addresses/3" for 127.0.0.1 at 2012-04-08 19:32:41 -0500
Processing by AddressesController#destroy as JS
  Parameters: {"id"=>"3"}
  Address Load (6.8ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."id" = ?   LIMIT 1  [["id", "3"]]
   (0.2ms)  begin transaction
  SQL (57.5ms)  DELETE FROM "addresses" WHERE "addresses"."id" = ?  [["id", 3]]
       (24.6ms)  commit transaction
Redirected to http://localhost:3000/addresses
Completed 302 Found in 108ms (ActiveRecord: 89.1ms)


Started GET "/addresses/3" for 127.0.0.1 at 2012-04-08 19:32:41 -0500
Processing by AddressesController#show as HTML
  Parameters: {"id"=>"3"}
  Address Load (0.1ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."id" = ?     LIMIT 1  [["id", "3"]]
Completed 500 Internal Server Error in 2ms

ActiveRecord::RecordNotFound (Couldn't find Address with id=3):
  app/controllers/addresses_controller.rb:16:in `show'


  Rendered /Users/<username>/.rvm/gems/ruby-1.9.3-preview1@railsnexttutorial/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.4ms)
  Rendered /Users/<username>/.rvm/gems/ruby-1.9.3-preview1@railsnexttutorial/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.2ms)
  Rendered /Users/<username>/.rvm/gems/ruby-1.9.3-preview1@railsnexttutorial/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.8ms)


Started DELETE "/addresses" for 127.0.0.1 at 2012-04-08 19:32:42 -0500

ActionController::RoutingError (No route matches [DELETE] "/addresses"):
  actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.2) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.2) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/static.rb:61:in `call'
  railties (3.2.2) lib/rails/engine.rb:479:in `call'
  railties (3.2.2) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.2) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /Users/<username>/.rvm/rubies/ruby-1.9.3-   preview1/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/<username>/.rvm/rubies/ruby-1.9.3-preview1/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/<username>/.rvm/rubies/ruby-1.9.3-preview1/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /Users/<username>/.rvm/gems/ruby-1.9.3-preview1@railsnexttutorial/gems/actionpack- 3.2.2/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)

编辑: 这是来自服务器的响应。我以为删除是删除而不是获取?

Started GET "/addresses" for 127.0.0.1 at 2012-04-07 21:26:13 -0500
Processing by AddressesController#index as HTML
  Address Load (0.9ms)  SELECT "addresses".* FROM "addresses" 
  Rendered addresses/index.html.erb within layouts/application (8.7ms)
Completed 200 OK in 47ms (Views: 22.2ms | ActiveRecord: 0.9ms)
[2012-04-07 21:26:14] WARN  Could not determine content-length of response body. Set     content-length of the response or set Response#chunked = true


Started GET "/addresses/1" for 127.0.0.1 at 2012-04-07 21:26:16 -0500
Processing by AddressesController#show as HTML
  Parameters: {"id"=>"1"}
  Address Load (0.3ms)  SELECT "addresses".* FROM "addresses" WHERE "addresses"."id" = ?      LIMIT 1  [["id", "1"]]
  Rendered addresses/show.html.erb within layouts/application (2.4ms)
Completed 200 OK in 7ms (Views: 4.6ms | ActiveRecord: 0.3ms)
[2012-04-07 21:26:16] WARN  Could not determine content-length of response body. Set    content-length of the response or set Response#chunked = true
4

2 回答 2

4

我有一个类似的问题。我的解决方案是:

1-确保 application.js 包含以下内容

//= require jquery
//= require jquery_ujs

2-确保 application.js 包含在我的应用程序布局中

= javascript_include_tag 'application'

3-删除旧版本的 JQuery 我也包括在我的应用程序布局中(这个让我有点困惑)

希望有帮助!

于 2012-04-28T09:32:51.997 回答
1

设置confirmdelete仅设置一些 data-* 属性。您可以在页面上使用一些 Javascript 来使用这些属性来创建所需的行为。没有任何 javascript 它只是一个普通的旧链接。

jquery-ujs库提供了这个。

于 2012-04-08T13:13:12.323 回答