diff --git a/app/assets/javascripts/bikes.js b/app/assets/javascripts/bikes.js index 29997d8..6d42c4f 100644 --- a/app/assets/javascripts/bikes.js +++ b/app/assets/javascripts/bikes.js @@ -2,7 +2,7 @@ $('.btn').button(); $("#add_bike_submit").click(function(){ - json_data = { bike: { + json_data = { bikes: [{ serial_number: $("#serial_number").val(), bike_brand_id: parseInt($("#bike_brand_id").val()), shop_id: parseInt($("#shop_id").val()), @@ -12,15 +12,16 @@ $("#add_bike_submit").click(function(){ bike_condition_id: parseInt($('input[name=bike_condition]:checked').val()), bike_purpose_id: 1, bike_wheel_size_id: parseInt($("#bike_wheel_size_id").val()), - }}; + }]}; $.ajax({ url: $("#add_bike_submit").data("url"), type: "POST", - data: json_data, + data: JSON.stringify(json_data), + contentType: 'application/json', dataType: "json", success: function(data, status, xhr){ - window.location = "/bikes/"+ data.bike.id; + window.location = data.bikes[0].href; }, error: function(data, status ){ displayFormErrors(data.responseJSON); diff --git a/app/controllers/api/v1/bikes_controller.rb b/app/controllers/api/v1/bikes_controller.rb index 116714b..6064bfd 100644 --- a/app/controllers/api/v1/bikes_controller.rb +++ b/app/controllers/api/v1/bikes_controller.rb @@ -5,17 +5,13 @@ class Api::V1::BikesController < Api::V1::BaseController before_filter :check_bike_permission def create - if bike = params[:bike] - - b = Bike.new(bike) - if !b.save - render json: { errors: b.errors }, status: 422 and return - else - render json: { bike: b.as_json }, status: 200 and return + if params[:bikes] && bike = params[:bikes].first + @bike = Bike.new(bike) + if !@bike.save + render json: { errors: @bike.errors }, status: 422 and return end - else - render json: { errors: ["Expected bike in submitted data" ]}, status: 422 and return + render json: { errors: [EXPECTED_BIKE]}, status: 422 and return end end diff --git a/config/routes.rb b/config/routes.rb index 4922638..b6485d4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,7 @@ Velocipede::Application.routes.draw do post 'reset' => "users#password_reset", :as => "api_password_reset" post 'bikes/create' => "bikes#create", as: "api_create_bike" + post 'bikes/:id' => "bikes#show", as: "api_bike" end end