How to raise a custom Exception in Rails 3+ with normal control flow? -


given following controller method:

  def create     @client = user.find(params[:client][:id])     respond_to |format|       if @client.present? && @client.add_manager(current_user)         format.html { redirect_to clients_path, notice: "successfully added manager" }       else         format.html { redirect_to clients_path, error: "could not find client" }       end     end   end 

how fail in else block instead of throwing runtimeerror turns "something went wrong" on production?

  def add_manager(user)     raise "already manager" if self.manager_users.include?(user)     self.manager_users << user if user.trainer?   end 

is code...

you can try this:

in controller

class yourappname::alreadymanagererror < standarderror  end 

now change "already manager" name of custom error

def add_manager(user)   raise yourappname::alreadymanagererror if self.manager_users.include?(user)   self.manager_users << user if user.trainer? end     

then in applicationcontroller

rescue_from yourappname::alreadymanagererror |exception|   render :nothing => "already manager", :status => 404 end 

this article goes more detail. check out rescue_from


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -