【问题标题】:How to construct delete route in link_to in rails 3.1?如何在 rails 3.1 的 link_to 中构造删除路线?
【发布时间】:2011-12-29 17:16:49
【问题描述】:

有 2 个模型 customer 和 comm_log。它们的关联如下:

resources :customers do
  comm_logs
end

comm_logs 控制器中用于销毁的 rspec 代码通过,没有任何错误。 lambda 块验证成功删除后通信日志计数减少了 1。控制器中的一切似乎都是正确的。

rake 路由的输出为:

       new_customer_comm_log GET    /customers/:customer_id/comm_logs/new(.:format)                 {:action=>"new", :controller=>"comm_logs"}
      edit_customer_comm_log GET    /customers/:customer_id/comm_logs/:id/edit(.:format)            {:action=>"edit", :controller=>"comm_logs"}
           customer_comm_log GET    /customers/:customer_id/comm_logs/:id(.:format)                 {:action=>"show", :controller=>"comm_logs"}
                             PUT    /customers/:customer_id/comm_logs/:id(.:format)                 {:action=>"update", :controller=>"comm_logs"}
                             DELETE /customers/:customer_id/comm_logs/:id(.:format)                 {:action=>"destroy", :controller=>"comm_logs"}

实际上,点击删除按钮后记录并没有被删除,页面也没有像在控制器中那样重定向到上一页(显示页面只是保留下来,删除后没有重定向到任何地方)。似乎删除操作被路由到了正确的路径。问题很可能出在视图中的下面的 link_to 上:

<%= link_to 'delete', customer_comm_log_path(@customer, @comm_log), :method => :delete, :confirm => "are you sure?", :remote => true %>

上面的link_to有什么问题吗?谢谢。

【问题讨论】:

  • 你能发布你的控制器销毁方法吗?

标签: ruby-on-rails ruby-on-rails-3.1


【解决方案1】:

您可能缺少一些东西。我假设您使用的是 jQuery,因为您没有在问题中指定。

a) 确保您已安装 jQuery

b) 确保您已经手动或使用 gem 安装了 Rails UJS(Unobtrusive Javascript)适配器(Rails 3.1 的说明也在此链接上):https://github.com/rails/jquery-ujs

c) 你的 link_to 需要告诉 Rails UJS 适配器它应该捕获链接的点击并通过 AJAX 提交 DELETE 请求

<%= link_to 'delete', customer_comm_log_path(@customer, @comm_log), :remote => true, :method => :delete) %>

所以在上面的link_to 中,你缺少:remote =&gt; true

Rails 将捕获用户对该链接的点击,然后启动对您的服务器的 jQuery $.ajax 调用,其中包括几个参数告诉 Rails 服务器这是一个远程 DELETE 请求。

d) 如果您通过用户单击这样的远程链接在控制器中进行重定向,您需要通过将此代码放入您的ApplicationController 来告诉 Rails 正确处理 AJAX 重定向。如果您不这样做,该链接将删除该记录,但随后它会坐在那里而不是重定向。

  # Allows redirecting for AJAX calls as well as normal calls
  def redirect_to(options = {}, response_status = {})
    if request.xhr?
      render(:update) {|page| page.redirect_to(options)}
    else
      super(options, response_status)
    end
  end

【讨论】:

  • 我正在运行 rails 3.1.0,jquery 似乎已经安装。 Gemfile 中有 gem 'jquery-rail'。在 application.js 中,有 require jquery 和 require jquery.ui。刚刚添加了 jquery.ujs。在帖子中添加了将 ajax 重定向到 application_controller.rb 的 def 代码。除了 :remote => true 之外,还在 link_to 中添加了 :confirm => true。单击删除时没有触发确认。也没有重定向。记录还在。
  • 服务器日志说什么(即请求是否曾经到达服务器)?此外,请检查浏览器控制台是否存在 javascript 错误。如果出现 JS 错误,则不会执行 $.ajax 调用。
  • 在lease_logs控制器中创建了一个new.js.erb文件并添加了html和js格式的respond_to后,上面发布的ajax destroy开始工作了。 new.js.erb 中有 2 行: $(" 'lease_logs/new.html.erb') %>").insertAfter('#new_log_link'); $('#new_log_link').hide();
  • 还将 destroy.js.erb 添加到视图中。它是一行文件 $("#").remove()
猜你喜欢
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多