I am playing with Rails 3.1.1 and made a simple blog app.
I created a unique index on a field and when I submit a new post, it
throws an exception if the field is not unique. I’ve added a rescue, but it’s not doing anything? What am I doing wrong.
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was
successfully created.’ } format.json { render json: @post, status: :created, location: @post } else format.html { render action: “new” } format.json { render json: @post.errors, status: :unprocessable_entity } end end rescue # SQLite3::ConstraintException render action: “new” end
SQLite3::ConstraintException in PostsController#create
column title is not unique
Rails.root: /home/yadav/dev/rails/tmp/blog
Application Trace | Framework Trace | Full Trace
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:84:in `close'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:84:in `dealloc'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:73:in `block in clear'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:72:in `each'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:72:in `clear'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:148:in `clear_cache!'
activerecord (3.1.1) lib/active_record/connection_adapters/sqlite_adapter.rb:142:in `disconnect!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:214:in `block in clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:213:in `each'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:213:in `clear_reloadable_connections!'
activesupport (3.1.1) lib/active_support/core_ext/module/synchronization.rb:35:in `block in clear_reloadable_connections_with_synchronization!'
/usr/local/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
activesupport (3.1.1) lib/active_support/core_ext/module/synchronization.rb:34:in `clear_reloadable_connections_with_synchronization!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:391:in `block in clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:391:in `each_value'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:391:in `clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in `clear_reloadable_connections!'
activerecord (3.1.1) lib/active_record/railtie.rb:84:in `block (3 levels) in <class:Railtie>'
activesupport (3.1.1) lib/active_support/callbacks.rb:404:in `_run_cleanup_callbacks'
activesupport (3.1.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:72:in `rescue in call'
actionpack (3.1.1) lib/action_dispatch/middleware/reloader.rb:67:in `call'
rack (1.3.5) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.5) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.5) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.5) lib/rack/lock.rb:15:in `call'
actionpack (3.1.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.1) lib/rails/engine.rb:456:in `call'
railties (3.1.1) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.5) lib/rack/handler/webrick.rb:59:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/usr/local/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/usr/local/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"c713B1JrYpXMZuH6DnwDAbD3itjj7MAZb1GfgHYLZ9g=",
"post"=>{"author"=>"Rajinder Yadav",
"title"=>"test post",
"message"=>"this is cool"},
"commit"=>"Create Post"}
_csrf_token: "c713B1JrYpXMZuH6DnwDAbD3itjj7MAZb1GfgHYLZ9g="
session_id: "a3784c99107db6ec649d06174043aa2f"
GATEWAY_INTERFACE: "CGI/1.1"
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
HTTP_ACCEPT_CHARSET: "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
HTTP_ACCEPT_ENCODING: "gzip, deflate"
HTTP_ACCEPT_LANGUAGE: "en-us,en;q=0.5"
REMOTE_ADDR: "127.0.0.1"
REMOTE_HOST: "localhost"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"
Response
Headers:
None