Manoj Sethi
Manoj Sethi

Reputation: 2038

RAILS API Endpoint

I am a .NET developer and need to work on a API built in Ruby. Following is the API Code. Can anybody help me in getting the endpoint to it.

 class Api::SessionsController < ApplicationController
   respond_to :json
   skip_before_filter :verify_authenticity_token

   before_filter :protect_with_api_key

   def update
     status = true
     participant_ids = []
     unless params[:participants].blank?
       params[:participants].each do |participant_data|
         participant = Participant.where(participant_id:                                  participant_data['participant_id']).first
       unless participant.present?
         status = false
         participant_ids << participant_data['participant_id']
       else
         activity_records = participant_data['cumulative_time']['activity_records']
         participant_data['cumulative_time']['activity_records'] = [] if  activity_records.nil?
         participant.participant_sessions.new(participant_data['cumulative_time'])
         participant.save!
       end
     end
   end 

   if status
     render :json => {status: "OK"}
   else
     render :json => {error: "No participant with id # {participant_ids.join(',')}"}, :status => 422
      end 
   end

 end

I have tried to work with following way /api/sessions/ Pass the required key passing the participant parameter with PUT Request like below

{"participants":[{"first_name":"Demo", "last_name":"User",     "email":"[email protected]", "password":"RubyNewBie","postal_code":"160055", "coordinator_name":"RubyNewBie", "coordinator_email":"info@RubyNewBie", "coordinator_phone":""}]}

Please guide me.

Thanks and Regards

Upvotes: 1

Views: 310

Answers (1)

Harfangk
Harfangk

Reputation: 843

By default, update action routes to /api/sessions/:id, so you should make query to that url. Also make sure that you have your route for session set up in routes.rb

Edit:

namespace :api do
  resources :participants do
    resources :sessions
  end
end

If it looks like that, then you're using nested routing. Check:

http://guides.rubyonrails.org/routing.html#nested-resources

You'll have to use the url /api/participants/:participant_id/sessions/:session_id under that setting.

Upvotes: 2

Related Questions