【问题标题】:Routing issue on Nested Resource Custom Update Method嵌套资源自定义更新方法的路由问题
【发布时间】:2016-03-03 14:30:48
【问题描述】:

我在 Rails 4 Ruby 2 中构建 Rails 应用程序

背景:

我在控制器中构建了一个自定义方法,允许我通过单击按钮来更新表格。

我在构建按钮时得到了一些帮助,但是它只更新嵌套元素中的第一个 id。

当我按下按钮以仅定位其所在的订单项时,我正在寻找所述按钮。

按钮/链接:

<%= link_to "Remove Unit", [@call, responding], class: 'btn btn-danger btn-sm', method: :delete, confirm: "Are you sure you want to remove this unit?" %><%= link_to "Responding", unit_responding_update_call_responding_path(call_id: @call.id, id: @respondings.first.id), method: :patch %>

此按钮显示在 &lt;%= @respondings.each do |responding| %&gt; 中,并且应该填充添加的每个行项目,以便在按下时可以更新该记录。

控制器动作是:

    def unit_responding_update
    @call = Call.find(params[:call_id])
    @responding = Responding.find(params[:id])
    @responding.responding_tme = DateTime.now
    @responding.responding = "true"
    @responding.on_scene = "false"
    @responding.clear = "false"
    @responding.save!
      respond_to do |format|
      if @responding.save
        format.html { redirect_to @call, notice: "Responding time successfully updated." }
      else
        format.html { render action: 'edit' }
      end
    end
   end

Routes.rb 是:

  resources :calls do
    resources :respondings, except: [:index], controller: 'calls/respondings' do
      member do
        patch :unit_responding_update
      end
    end
      resources :pings, except: [:index], controller: 'calls/pings'
      resources :agencies, except: [:index], controller: 'calls/agencies'
      resources :incidents, except: [:index], controller: 'calls/incidents'
      resources :complainants, except: [:index], controller: 'calls/complainants'
    end
  end

最后,自定义操作的 Rake Routes 的输出是:

 unit_responding_update_call_responding PATCH  /calls/:call_id/respondings/:id/unit_responding_update(.:format) calls/respondings#unit_responding_update

我知道这可能只是一个小问题,我查看了类似的 StackOverflow 问题、树屋论坛和代码学院资源,但我终生无法解决这个问题..

提前感谢您的帮助。如果您需要任何进一步的信息,请告诉我。

【问题讨论】:

    标签: ruby ruby-on-rails-4 routes nested-routes


    【解决方案1】:

    但是它只更新嵌套元素中的第一个 id。

    这是由

    引起的

    &lt;%= link_to "Responding", unit_responding_update_call_responding_path(call_id: @call.id, id: @respondings.first.id), method: :patch %&gt;

    因为这个按钮显示在 ,替换为

    &lt;%= link_to "Responding", unit_responding_update_call_responding_path(call_id: @call.id, id: responding.id), method: :patch %&gt;

    【讨论】:

    • 太棒了!所以这是我在响应时出错的 id 呼叫!非常感谢您的快速回复!
    • 另外也不需要使用&lt;%= @respondings.each do |responding| %&gt;,你可以使用&lt;% @respondings.each do |responding| %&gt;而不使用=,因为这部分代码不会在呈现的html上输出任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    相关资源
    最近更新 更多