Content negotiation cho phép một action phản hồi theo format khác nhau (HTML, JSON, XML) tùy Accept header hoặc URL extension.
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # render app/views/posts/show.html.erb
format.json { render json: @post }
format.xml { render xml: @post }
end
end
endRequest GET /posts/1 → HTML. GET /posts/1.json hoặc header Accept: application/json → JSON.
respond_with (từ gem responders) ngắn gọn hơn cho CRUD chuẩn nhưng ít tùy biến hơn. Với Rails API mode (ActionController::API), response mặc định là JSON và không cần respond_to.
Content negotiation lets a single action respond in different formats (HTML, JSON, XML) based on the Accept header or URL extension.
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # renders app/views/posts/show.html.erb
format.json { render json: @post }
format.xml { render xml: @post }
end
end
endGET /posts/1 → HTML. GET /posts/1.json or Accept: application/json header → JSON.
respond_with (via the responders gem) is more concise for standard CRUD but less flexible. In Rails API mode (ActionController::API), JSON is the default and respond_to is usually unnecessary.