ruby-on-railsrestnested-attributes

Rails, updating multiple nested attributes RESTfully


I've got a question about updating attributes.

I have an user model and also a debt model. How do I make an restful approach to update all debts belonging to a specific user (users/:user_id/debts) so that they are marked as paid (a boolean attribute) from a link on the site?


Solution

  • You cant fit this action into the 7 REST actions, you have to create a new collection method on the debts model.

    #in routes.rb
    
    resources :users do
      resources :debts do
        post 'bulk_update', :on => :collection
      end
    end
    

    In your debts_controller.rb

    def bulk_update
      @user = User.find(params[:user_id])
      @debts = @user.debts
      #etc...
    end
    

    In your views

    link_to "Update user's debts", bulk_update_user_debts_path(@user), :method=>:post
    

    Something like this, you can add your optional parameters as well to fit in to your task.