0

Im Building out a Rails app in Rails 4 Ruby 2

Background:

I have built a custom Method in a controller that allows me to update a table by button click.

I had some help building out the button, however it only updates the first id, in the nested element.

I am looking for said button when pushed to target only the line item its sitting in.

The button/link:

<%= 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 %>

this button is displayed with in a <%= @respondings.each do |responding| %> and should populate with each line item that is added, so that it can update that record when pushed.

the controller action is:

    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 is:

  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

And finally the output for Rake Routes for the custom action is:

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

I know its probably only a small problem, ive looked at similar StackOverflow Questions, treehouse forums and code academy resources but can not for the life of me sort this one out..

Thanks in advance for your assistance. Please lte me know if you require anything further for information.

Shawn Wilson
  • 1,311
  • 14
  • 40

1 Answers1

1

however it only updates the first id, in the nested element.

this is caused by

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

As this button is displayed with in a <%= @respondings.each do |responding| %>, replace with

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

richfisher
  • 951
  • 6
  • 6
  • Fantastic! so it was the id call on responding that i was going wrong on! Thanks so much for the quick reply! – Shawn Wilson Nov 30 '15 at 03:37
  • Also there isn't a need to use `<%= @respondings.each do |responding| %>`, you can use `<% @respondings.each do |responding| %>` without `=`, as this part of code doesn't output anything on the html that is rendered. – Raman Nov 30 '15 at 07:51