How to Silence The Call to a Rails Controller's Action All Together

How to silence the call to a rails controller's action all together

Late answer, but I spent quite a bit of time of the interwebs looking for the answer, which is thus:

In the controller that contains the action you would like to ignore, override the logger method and return the rails default for some actions, and a custom logger or nil for the ones that need special handling.

def logger
if params[:action] == 'action_to_ignore'
# return custom logger or nil
else
RAILS_DEFAULT_LOGGER
end
end

I suspect there is an easier way, and would love to hear it if it exists.

How do you suppress logging for one particular action?

  1. Look here: How to silence the call to a rails controller's action all together and here: How can I disable logging in Ruby on Rails on a per-action basis?

  2. You can look deeper into Rack to optimize your application. If javascript poll returns simple data it will fit perfectly for you

Suppress Rails logging for specific ActiveRecord request

Silence

I don't have a specific answer for your question, but we use ActiveRecord::Base.logger.silence to silence the logger:

    ActiveRecord::Base.logger.silence do
# your method here
end

It's not for specific requests, but works for actions you may wish to stop logging. Perhaps you could extend ActiveRecord::Base to silence a specific method?

--

Extending ActiveRecord

Perhaps you could mix what I've described with extending ActiveRecord? The recommendation on this question is to use an ActiveRecord concern.

I've never used one of these - can put together some code if you feel applicable

How can I disable logging in Ruby on Rails on a per-action basis?

The answer turns out to be a lot harder than I expected, since rails really does provide no hook to do this. Instead, you need to wrap some of the guts of ActionController::Base. In the common base class for my controllers, I do

def silent?(action)
false
end

# this knows more than I'd like about the internals of process, but
# the other options require knowing even more. It would have been
# nice to be able to use logger.silence, but there isn't a good
# method to hook that around, due to the way benchmarking logs.

def log_processing_with_silence_logs
if logger && silent?(action_name) then
@old_logger_level, logger.level = logger.level, Logger::ERROR
end

log_processing_without_silence_logs
end

def process_with_silence_logs(request, response, method = :perform_action, *arguments)
ret = process_without_silence_logs(request, response, method, *arguments)
if logger && silent?(action_name) then
logger.level = @old_logger_level
end
ret
end

alias_method_chain :log_processing, :silence_logs
alias_method_chain :process, :silence_logs

then, in the controller with the method I want to suppress logging on:

def silent?(action)
RAILS_ENV == "development" && ['my_noisy_action'].include?(action)
end

how to handle ActiveRecord::RecordNotFound in rails controller?

Put return after redirect

begin
@userEvents = current_user.event
@event = @userEvents.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to :controller => "main", :action => "index"
return
end

Rails: call another controller action from a controller

You can use a redirect to that action :

redirect_to your_controller_action_url

More on : Rails Guide

To just render the new action :

redirect_to your_controller_action_url and return

Differences in rails between new + save and create

Internally create calls new then save anyway:

  def create(attributes = nil, options = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, options, &block) }
else
object = new(attributes, options, &block)
object.save
object
end
end

Rails 3 disabling session cookies

As is mentioned in a comment on John's answer, clearing the session will not prevent the session cookie from being sent. If you wish to totally remove the cookie from being sent, you have to use Rack middleware.

class CookieFilter
def initialize(app)
@app = app
end

def call(env)
status, headers, body = @app.call(env)

# use only one of the next two lines

# this will remove ALL cookies from the response
headers.delete 'Set-Cookie'
# this will remove just your session cookie
Rack::Utils.delete_cookie_header!(headers, '_app-name_session')

[status, headers, body]
end
end

Use it by creating an initializer with the following body:

Rails.application.config.middleware.insert_before ::ActionDispatch::Cookies, ::CookieFilter

To prevent the cookie filter to end up in application stack traces, which can be utterly confusing at times, you may want to silence it in the backtrace (Assuming you put it in lib/cookie_filter.rb):

Rails.backtrace_cleaner.add_silencer { |line| line.start_with? "lib/cookie_filter.rb" }


Related Topics



Leave a reply



Submit