मैं कुछ छात्रों के लिए एपीआई बनाने के तरीके पर एक साधारण रेल ट्यूटोरियल का निर्माण कर रहा हूं और मैं इसे answer_to और respond_with के बिना बना रहा हूं क्योंकि मैं सिर्फ यह देखना चाहता हूं कि मैं एक मणि का उपयोग किए बिना एपीआई बना सकता हूं या नहीं।रेल एपीआई में respond_with/to के बजाय रेंडर का उपयोग करने के बीच क्या अंतर है?
नियंत्रक:
class Api::V1::SuyasController < ApplicationController
def index
render json: Suya.all
end
def create
render json: Suya.create(suyas_params)
end
private
def suyas_params
params.require(:suya).permit(:meat, :spicy)
end
end
मार्गों:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :vendors
resources :suyas
end
end
end
परीक्षण: यह है कि मैं क्या है और मेरे परीक्षण पास है
require 'test_helper'
class Api::V1::SuyasControllerTest < ActionController::TestCase
test "index can get all the suyas" do
Suya.create(meat: "beef", spicy: true)
Suya.create(meat: "kidney", spicy: false)
get :index
suyas = JSON.parse(response.body)
assert_equal "beef", suyas[0]["meat"]
assert_equal true, suyas[0]["spicy"]
assert_equal "kidney", suyas[1]["meat"]
assert_equal false, suyas[1]["spicy"]
end
test "create can create a suya" do
assert_difference("Suya.count", 1) do
create_params = { suya: { meat: "beefy", spicy: true }, format: :json }
post :create, create_params
suya = JSON.parse(response.body)
assert_equal "beefy", suya["meat"]
assert_equal true, suya["spicy"]
end
end
end
क्या respond_with बनाम प्रस्तुत करना का उपयोग कर के बीच अंतर है? मुझे कोई जवाब नहीं मिल रहा है। क्या ऐसा कुछ भी है जो मैं गलत कर रहा हूं? क्यों एपीआई बनाने के दो तरीके देखते हैं (respond_to/respond_with और इस तरह से?)
जेफ
आपको किस समस्या का सामना करना पड़ रहा है? – Ojash
आपके परिप्रेक्ष्य से कोड के साथ क्या गलत है? आपको क्या त्रुटियां मिल रही हैं? – Makoto
असल में, ऐसा करने के दो तरीके क्यों हैं? मेरे रास्ते और दूसरे के बीच क्या अंतर है? मेरे परीक्षण पास और सबकुछ ... मैं सिर्फ दोनों तरीकों से नहीं जानता हूं। – Jwan622