嵌套资源自定义更新方法的路由问题



Im在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 %>

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

控制器动作为:

    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问题、树屋论坛和代码学院资源,但我一辈子都无法解决这个问题。。

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

然而,它只更新嵌套元素中的第一个id。

这是由引起的

<%= link_to "Responding", unit_responding_update_call_responding_path(call_id: @call.id, id: @respondings.first.id), method: :patch %>

由于此按钮显示为<%=@responsing.each do | responsing |%>,替换为

<%= link_to "Responding", unit_responding_update_call_responding_path(call_id: @call.id, id: responding.id), method: :patch %>

最新更新