Kiran Patil
Kiran Patil

Reputation: 429

rails flash alerts from api controller

I have api controller as below and tried to add flash alert and it is not working.

Please throw some light on this issue.

class API::V1::SmsController < ApplicationController
  respond_to :json

  def incoming_sms
   # respond_with User.find(params[:id])
   contact = LocationMessage.new
   contact.number = params[:number]

   message = params[:message]

   contact.latitude= message.split(':')[1].to_s
   contact.longitude= message.split(':')[2].to_s
   contact.address= message.split(':')[3].to_s

   if contact.save
          flash[:notice] = "New Sms arrived in your System."

          render :json =>"Location Message saved", status: 201
   else
          render json: { errors: contact.errors}, status: 422
   end
  end

Thanks, Kiran.

Upvotes: 2

Views: 1147

Answers (1)

Maru
Maru

Reputation: 600

Flash messages in general don't play well with api controllers since they are typically rendered into directly into views as opposed to something that goes hand in hand with json responses

APIs dont render views or directly reload pages so flash messages will not be rendered into the page. and because of the nature of flash mentioned in the documentation, even if you refresh the page after a successful ajax call, the flash message would no longer be persisted

Upvotes: 1

Related Questions