How to update Rails Controller to return an error when Model.create errors?

Viewed 2265

I have the following controller:

class Api::V1::FeedbacksController < ApplicationController

  before_action :authenticate_user!

  def create

    @feedback = current_user.feedbacks.create(
      feedback_type: params[:selectedType],
      message: params[:message]
    )

    json_response(@feedback)
  end

private

  def json_response(object, status = :ok)
    render json: object, status: status
  end

end

Feedback.rb validates :message, presence: true, length: { in: 1..1000 }

This works great when message is between 1 to 1000 in length. If the controller is submitted more than 1000 characters, the controller is still respond back but without the error.

What is the right way in Rails 5 to have the controller return an error if the create method above fails?

1 Answers
Related