Rails 3.2.2 + jquery mobile:方法:删除不工作

时间:2012-04-08 00:05:59

标签: ruby-on-rails ruby-on-rails-3 jquery-mobile

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

# 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: 的 我添加了&lt;%= javascript_include_tag'应用'%&gt;到我的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)

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

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

2 个答案:

答案 0 :(得分:4)

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

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

//= require jquery
//= require jquery_ujs

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

= javascript_include_tag 'application'

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

希望有所帮助!

答案 1 :(得分:1)

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

jquery-ujs库提供此功能。