Monthly Archives: July 2014

Redirecting exception to controller’s index page

I want to improve my appliations error handling by capturing common errors and just redirecting to the index page for th particular controller with a flash message.

Most suggestions I have come across suggest url_for or redirect_to but specify either root_url or a specific controller (usually post!), but redirecting to the root_url can be very confusing when you’re working with 15 models.

As ever, the Stack comes to the rescue: http://stackoverflow.com/questions/9912678/rails-how-to-request-url-except-url-params provides enough of a hint as how to proceed. The error in the logs caused by the first suggestion gave enough of a clue for a workable solution.

In app/controllers/application_controller.erb

ActionView::Template::Error, with: :template_error
...
protected
def template_error(exception)
  session.delete :search
  redirect_to "/"+request.path.split('/').second, :alert => "Template error: #{exception.message}."
end

I could possibly even write my own helper to tidy this up a bit more. I suspect there are tidier ways of working this if I give a little more thought but at least I have something that works for now.

(I) always live on the server-side of life

More posting as a reminder to myself to save looking up on the Stack when I need reminding again.

== To capture the value of an selected select form item,
var c = $(“#object_id option:selected”).val();

== To capture the option text of an selected select form item,
var t = $(“#object_id option:selected”).text();

== To dynamically add an option to a select list,
$(“#object_id”).append($(”).val(‘value’).html(‘option text’));

== To mark a specific option in a list as selected,
$(“#object_id”).val(value).attr(‘selected’, ‘selected’);

or

$(“#phone_archiving_policy_id option[value='”+value+”‘]”).attr(‘selected’, ‘selected’);

Would like to say that I’d figured this out myself but while JavaScript (and particularly JQuery) enable some very impressive effects I still prefer the server-side of life and defer to those on fora to advise on dynamic visual features; web design just kills me.