RAILS API 端点
RAILS API Endpoint
我是 .NET 开发人员,需要在 Ruby 内置的 API 上工作。以下是 API 代码。任何人都可以帮助我获得它的端点。
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
我尝试过以下方式 /api/sessions/
传递所需的密钥
通过
PUT 请求的参与者参数如下
{"participants":[{"first_name":"Demo", "last_name":"User", "email":"demouser@demouser.com", "password":"RubyNewBie","postal_code":"160055", "coordinator_name":"RubyNewBie", "coordinator_email":"info@RubyNewBie", "coordinator_phone":""}]}
请指导我。
感谢和问候
默认情况下,将操作路由更新到 /api/sessions/:id
,因此您应该查询 url。还要确保您在 routes.rb
中设置了会话路线
编辑:
namespace :api do
resources :participants do
resources :sessions
end
end
如果看起来像那样,那么您正在使用嵌套路由。检查:
http://guides.rubyonrails.org/routing.html#nested-resources
您必须在该设置下使用 url /api/participants/:participant_id/sessions/:session_id
。
我是 .NET 开发人员,需要在 Ruby 内置的 API 上工作。以下是 API 代码。任何人都可以帮助我获得它的端点。
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
我尝试过以下方式 /api/sessions/ 传递所需的密钥 通过 PUT 请求的参与者参数如下
{"participants":[{"first_name":"Demo", "last_name":"User", "email":"demouser@demouser.com", "password":"RubyNewBie","postal_code":"160055", "coordinator_name":"RubyNewBie", "coordinator_email":"info@RubyNewBie", "coordinator_phone":""}]}
请指导我。
感谢和问候
默认情况下,将操作路由更新到 /api/sessions/:id
,因此您应该查询 url。还要确保您在 routes.rb
编辑:
namespace :api do
resources :participants do
resources :sessions
end
end
如果看起来像那样,那么您正在使用嵌套路由。检查:
http://guides.rubyonrails.org/routing.html#nested-resources
您必须在该设置下使用 url /api/participants/:participant_id/sessions/:session_id
。