Skip to content

Instantly share code, notes, and snippets.

@alexfedoseev
Last active Dec 15, 2015
Embed
What would you like to do?
Rails API controller: iteration 1
# app/controllers/api/v1/comments_controller.rb
class Api::V1::CommentsController < ApplicationController
def index
@comments = Comment.all.order(:id).reverse
respond_with @comments
end
def show
@comment = Comment.find(params[:id])
respond_with @comment
end
def create
@comment = Comment.new(comment_params)
if @comment.save
render json: @comment, status: 201
else
render json: { errors: @comment.errors.full_messages }, status: 422
end
end
private
def comment_params
params.require(:comment).permit(:author, :comment)
end
end
@aqabawe

This comment has been minimized.

Copy link

@aqabawe aqabawe commented Oct 31, 2015

I had to add include ActionController::ImplicitRender to prevent a NoMethodError (undefined method 'default_render') error as per this issue.

@alexfedoseev

This comment has been minimized.

Copy link
Owner Author

@alexfedoseev alexfedoseev commented Dec 15, 2015

@aqabawe I finally checked this. It's better to add it to ApplicationController (link), so you don't have to re-add it to everytime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment