I created a rails API using postgre database in which i have a model (table) name as counseling_event
NOTE: Not using scaffold and using rails-5
My scheman(migration) for counseling_event is as follow:
class CreateCounselingEvent < ActiveRecord::Migration[5.0]
def change
create_table :counseling_event do |t|
t.text :name
t.datetime :start_time
t.datetime :end_time
t.text :location
t.integer :user_id
t.integer :role_id
t.timestamps
end
end
end
I added a column to the above table, which is of type enum. column name is event_type
Now, In my Controller i.e CounselingEventController, in my action index i'm retrieving the data from the counseling_event table. the code is as follows.
def index
#counseling_event = CounselingEvent.where(start_time: "2016-10-30".."2016-12-11")
render body: #counseling_event
end
when i call this api controller action from frontend through following route
get 'counseling_event/index'
I'm getting this error in my console:
CounselingEvent Load (1.5ms) SELECT "counseling_event".* FROM "counseling_event"
NoMethodError (undefined method `empty?' for #<CounselingEvent:0x47d1d58>):
activemodel (5.0.0.1) lib/active_model/attribute_methods.rb:433:in `method_missing'
rack (2.0.1) lib/rack/etag.rb:68:in `block in digest_body'
activerecord (5.0.0.1) lib/active_record/relation/delegation.rb:38:in `each'
activerecord (5.0.0.1) lib/active_record/relation/delegation.rb:38:in `each'
actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:145:in `each_chunk'
actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:126:in `each'
actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:74:in `each'
actionpack (5.0.0.1) lib/action_dispatch/http/response.rb:464:in `each'
rack (2.0.1) lib/rack/etag.rb:66:in `digest_body'
rack (2.0.1) lib/rack/etag.rb:29:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
activerecord (5.0.0.1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.1) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
railties (5.0.0.1) lib/rails/engine.rb:522:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `call'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'
I don't know the reason for the error but, trying hard to resolve it. can any one help me out to solve this error. it seems like some activemodel error. Thanks in advance.
Rails is based on Rack. Rack can only handle strings. You're rendering an object, not a string.
You probably want something like:
render plain: #counceling_event.to_s
It basically happens because Rack attempts to call #empty? on the body of the response to determine how it should respond to the client.
But since your #counceling_event isn't a string and doesn't respond to empty? it fails.
I'm unsure what you're trying to do by render body: <SOMETHING>. If you're on a Rails app you probably want to render a view instead of content directly from the controller.
If you really want to render from the controller you should consider using render plain: <SOMETHING>.
Hope this helps.
Related
We have a webap that was initially born in 2.5 ruby.
We have upgraded to 2.6 and then, we have a problem to serve its pages.
Here is the error we receive:
NoMethodError in SingleviewController#index
undefined method `[]' for nil:NilClass
Extracted source (around line #36):
34
35
36
37
38
39
end
unless headers[CACHE_CONTROL]
if digest
headers[CACHE_CONTROL] = #cache_control if #cache_control
else
Application Trace | Framework Trace | Full Trace
rack (2.0.8) lib/rack/etag.rb:36:in `call'
rack (2.0.8) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.8) lib/rack/head.rb:12:in `call'
actionpack (6.0.2.2) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.8) lib/rack/session/abstract/id.rb:259:in `context'
rack (2.0.8) lib/rack/session/abstract/id.rb:253:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.2.2) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.2.2) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.2.2) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.2.2) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.2.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.8) lib/rack/method_override.rb:22:in `call'
rack (2.0.8) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
rack-livereload (0.3.17) lib/rack/livereload.rb:23:in `_call'
rack-livereload (0.3.17) lib/rack/livereload.rb:14:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.0.8) lib/rack/sendfile.rb:111:in `call'
actionpack (6.0.2.2) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
webpacker (4.2.2) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.2.2) lib/rails/engine.rb:526:in `call'
puma (4.3.5) lib/puma/configuration.rb:228:in `call'
puma (4.3.5) lib/puma/server.rb:713:in `handle_request'
puma (4.3.5) lib/puma/server.rb:472:in `process_client'
puma (4.3.5) lib/puma/server.rb:328:in `block in run'
puma (4.3.5) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
I am a newby. What else do you need to find my problem?
I really don't know where to start looking at.
Best regards.
Based on the provided code sample, it looks like your headers method is returning nil for some reason. I would try two things:
First, change headers to request.headers
If that doesn't work, change your code to something like:
begin
unless headers[CACHE_CONTROL]
if digest
headers[CACHE_CONTROL] = #cache_control if #cache_control
else
...
rescue
# You should probably do something more meaningful here.
# This is only meant to return a rescue value for this design pattern
nil
end
I'm following a sortable list tutorial in here , but unfortunately the article is bit old (but very useful) and I'm having some issues when try to implement it with
Rails 3.2.x
coffeescript
Jquery
My issue is , view is not passing request to controller and instead gives a routing error
Following is my view
#index.html.erb
<h1>Listing books</h1>
<ul id="books"> <% #books.each do |book| %>
<li class="book<%= book.id %>"><span class="handle">[drag]</span><%= book.name %></li>
<% end %></ul>
<%= link_to 'New book', new_book_path %>
and this is my coffeescript
#books.js.coffee
jQuery ->
$('#books').sortable
axis: 'y'
handle: '.handle'
update: ->
$.post($(this).data('#books'), $(this).sortable('serialize'))
and this is my controller
#books_controller.rb
def sort
#books = Book.all
#books.each do |book|
book.position = params['book'].index(book.id.to_s) + 1
book.save
end
render :nothing => true
end
and this is my routes
#config/routes.rb
resources :books do
collection do
post :sort
end
end
this is the error I'm getting
Started POST "/undefined" for 127.0.0.1 at 2012-11-28 16:13:29 +0530
ActionController::RoutingError (No route matches [POST] "/undefined"):
actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.9) lib/rails/engine.rb:479:in `call'
railties (3.2.9) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/sameera/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/sameera/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/sameera/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
Rendered /home/sameera/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.7ms)
Any help will be greatly appreciated,
thanks
I believe $.post requires a url parameter like this:
$.post('/books/sort', $(this).data('#books'), $(this).sortable('serialize'))
I am developing a platform that allows merchants to upload a picture of their store. I am using Carrierwave+fog for this. As the app is hosted on heroku, the pictures will need to be stored on amazon s3.
On my local machine, everything works fine if the storage is local, but things go wrong once fog handles the uploading.
I will first post the local storage version (works fine), then my current fog-enabled s3 version together with the error that I am getting.
Local storage version (works fine):
# app/uploaders/picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
Fog-enabled s3 version (doesn't work):
# app/uploaders/picture_uploader.rb
class PictureUploader < CarrierWave::Uploader::Base
storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
# config/initializers/fog.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => 'xxxx', # required
:aws_secret_access_key => 'xxxx' # required
}
config.fog_directory = 'yyyy' # required
config.fog_public = true # optional, defaults to true
end
When I now create or edit a new store, the picture is actually successfully uploaded to s3, but rails gives an Permission denied error message (app/tmp/carrierwave/20121104-0929-5512-8270/04.png could not be accessed).
Full Error Message:
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1406:in `unlink'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1406:in `block in remove_file'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1414:in `platform_support'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:1405:in `remove_file'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:785:in `remove_file'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:563:in `block in rm'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:562:in `each'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/fileutils.rb:562:in `rm'
carrierwave (0.5.8) lib/carrierwave/sanitized_file.rb:217:in `delete'
carrierwave (0.5.8) lib/carrierwave/uploader/store.rb:60:in `block in store!'
carrierwave (0.5.8) lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
carrierwave (0.5.8) lib/carrierwave/uploader/store.rb:58:in `store!'
carrierwave (0.5.8) lib/carrierwave/mount.rb:347:in `store!'
carrierwave (0.5.8) lib/carrierwave/mount.rb:217:in `store_bild!'
activesupport (3.1.0) lib/active_support/callbacks.rb:401:in `_run_save_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.1.0) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.1.0) lib/active_record/persistence.rb:37:in `save'
activerecord (3.1.0) lib/active_record/validations.rb:50:in `save'
activerecord (3.1.0) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.1.0) lib/active_record/transactions.rb:241:in `block (2 levels) in save'
activerecord (3.1.0) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/transactions.rb:241:in `block in save'
activerecord (3.1.0) lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
activerecord (3.1.0) lib/active_record/transactions.rb:240:in `save'
activerecord (3.1.0) lib/active_record/persistence.rb:163:in `block in update_attributes'
activerecord (3.1.0) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.1.0) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.1.0) lib/active_record/persistence.rb:161:in `update_attributes'
app/controllers/stores_controller.rb:62:in `block in update'
actionpack (3.1.0) lib/action_controller/metal/mime_responds.rb:269:in `call'
actionpack (3.1.0) lib/action_controller/metal/mime_responds.rb:269:in `retrieve_response_from_mimes'
actionpack (3.1.0) lib/action_controller/metal/mime_responds.rb:194:in `respond_to'
app/controllers/stores_controller.rb:61:in `update'
actionpack (3.1.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.0) lib/active_support/callbacks.rb:416:in `_run__16358980__process_action__192134115__callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.0) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.0) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.0) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.0) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.0) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.0) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.0) lib/action_dispatch/routing/route_set.rb:531:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.6) lib/rack/etag.rb:23:in `call'
rack (1.3.6) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/flash.rb:243:in `call'
rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/cookies.rb:326:in `call'
activerecord (3.1.0) lib/active_record/query_cache.rb:62:in `call'
activerecord (3.1.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.0) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.0) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.0) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
actionpack (3.1.0) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.0) lib/rails/engine.rb:455:in `call'
railties (3.1.0) lib/rails/rack/content_length.rb:16:in `call'
railties (3.1.0) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
This error confuses me, as typing in app/tmp/carrierwave/20121104-0929-5512-8270/04.png directly into the browser works fine.
For completeness sake, here is the controller & model (same in both versions)
#stores_controller.rb
def create
#store = Store.new(params[:store])
respond_to do |format|
if #store.save
format.html { redirect_to #store, notice: 'Store was successfully created.' }
format.json { render json: #store, status: :created, location: #store }
else
format.html { render action: "new" }
format.json { render json: #store.errors, status: :unprocessable_entity }
end
end
end
#models/store.rb
class Store < ActiveRecord::Base
mount_uploader :bild, PictureUploader
end
Try adding cache_dir to your uploader:
def cache_dir
"#{Rails.root}/tmp/uploads"
end
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
Also, you might have to add config/initializers/carrierwave.rb:
CarrierWave.configure do |config|
# hack fix for windows machine due to tmp file permission error
# per https://github.com/jnicklas/carrierwave/issues/220/
if ENV['RAILS_ENV'] != 'production'
config.delete_tmp_file_after_storage = false
end
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => ENV['MF_S3_KEY'], # required
:aws_secret_access_key => ENV['MF_S3_SECRET'], # required
:region => 'us-east-1' # optional, defaults to 'us-east-1'
}
config.fog_directory = ENV['MF_FLYER_BUCKET'] # required
#config.fog_host = 'https://assets.example.com' # optional, defaults to nil
#config.fog_public = false # optional, defaults to true
#config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
I can't seem to get CanCan working. This has turned out to be quite frustrating, especially when I have followed the rule book and tried, seemingly, all the tricks under the sun to get it to work.
I am using Rails 3.1.2, Devise 2.1.2, and CanCan 1.6.8. I set-up a separate role model as described by RyanB here.
This is my code.
1) ability.rb
class Ability
include CanCan::Ability
def initializer(user)
user ||= User.new # For users with no account
if (user.has_role?(:admin)) || (user.has_role?(:mod))
can :manage, :all
elsif user.has_role?(:registered)
can :read, :all
can :create, :all
can :update, Question do |question|
question.try(:user) == (user || user.has_role?(:admin) || user.has_role?(:mod))
end
can :destroy, Question do |question|
question.try(:user) == (user || user.has_role?(:admin) || user.has_role?(:mod))
end
else
can :read, :all
end
end
end
2) has_role? definition in user.rb
def has_role?(role_sym)
roles.any? { |r| r.name.underscore.to_sym == role_sym }
end
3) Controller before filters
before_filter :authenticate_user!, :except => :show
load_and_authorize_resource
skip_authorize_resource :only => :show
Authorizing any controller with CanCan results in the same error on all actions.
EDIT: Stack Trace
cancan (1.6.8) lib/cancan/controller_additions.rb:352:in `initialize'
cancan (1.6.8) lib/cancan/controller_additions.rb:352:in `new'
cancan (1.6.8) lib/cancan/controller_additions.rb:352:in `current_ability'
cancan (1.6.8) lib/cancan/controller_resource.rb:207:in `current_ability'
cancan (1.6.8) lib/cancan/controller_resource.rb:77:in `load_collection?'
cancan (1.6.8) lib/cancan/controller_resource.rb:33:in `load_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancan (1.6.8) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
activesupport (3.2.1) lib/active_support/callbacks.rb:429:in `_run__458654864__process_action__156152648__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.1) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/params_wrapper.rb:205:in `process_action'
activerecord (3.2.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.1) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.1) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.1) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:66:in `dispatch'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:30:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/cookies.rb:338:in `call'
activerecord (3.2.1) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:443:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `_run__446710069__call__461654473__callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.1) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.1) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.1) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.1) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.2.1) lib/rails/engine.rb:479:in `call'
railties (3.2.1) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/home/administrator/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/home/administrator/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/home/administrator/.rvm/rubies/ruby-1
.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Okay, it seems, I did not followed ALL the rules of the rule book. Although I cannot see how this could be a problem. To get this working, I deleted my ability.rb file that I created manually, and used the rails g cancan:ability command to generate the same file in the same directory, and used the same code. And now it works like a charm.
I am calling the index action for a class which gives the following error:
Started GET "/images" for 127.0.0.1 at Sun Aug 28 06:14:41 +0200 2011
Processing by ImagesController#index as HTML Completed 500 Internal
Server Error in 1ms
ArgumentError (wrong number of arguments (2 for 0)):
There is nothing in the application trace, meaning that the failure occurs before any of the files that I created for app get processed. The full trace is here:
activesupport (3.0.8) lib/active_support/callbacks.rb:97:in `send'
activesupport (3.0.8) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (3.0.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
activesupport (3.0.8) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.8) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.8) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.8) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.8) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.8) lib/abstract_controller/rendering.rb:41:in `process'
actionpack (3.0.8) lib/action_controller/metal.rb:138:in `dispatch'
actionpack (3.0.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.8) lib/action_controller/metal.rb:178:in `action'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:148:in `call'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:93:in `recognize'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:82:in `optimized_each'
rack-mount (0.6.14) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.14) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.8) lib/action_dispatch/routing/route_set.rb:493:in `call'
sass (3.1.7) lib/sass/./sass/plugin/rack.rb:54:in `call'
warden (1.0.5) lib/warden/manager.rb:35:in `call'
warden (1.0.5) lib/warden/manager.rb:34:in `catch'
warden (1.0.5) lib/warden/manager.rb:34:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.3) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/cookies.rb:302:in `call'
activerecord (3.0.8) lib/active_record/query_cache.rb:32:in `call'
activerecord (3.0.8) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.8) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.8) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/callbacks.rb:46:in `call'
activesupport (3.0.8) lib/active_support/callbacks.rb:419:in `_run_call_callbacks'
actionpack (3.0.8) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.3) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.0.8) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.3) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
rack (1.2.3) lib/rack/lock.rb:11:in `synchronize'
rack (1.2.3) lib/rack/lock.rb:11:in `call'
actionpack (3.0.8) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.8) lib/rails/application.rb:168:in `call'
railties (3.0.8) lib/rails/application.rb:77:in `send'
railties (3.0.8) lib/rails/application.rb:77:in `method_missing'
railties (3.0.8) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.3) lib/rack/content_length.rb:13:in `call'
rack (1.2.3) lib/rack/handler/webrick.rb:52:in `service'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:162:in `start'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:95:in `start'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:92:in `each'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:92:in `start'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:23:in `start'
/home/jon/.rvm/rubies/ruby-1.8.7-p334/lib/ruby/1.8/webrick/server.rb:82:in `start'
rack (1.2.3) lib/rack/handler/webrick.rb:13:in `run'
rack (1.2.3) lib/rack/server.rb:217:in `start'
railties (3.0.8) lib/rails/commands/server.rb:65:in `start'
railties (3.0.8) lib/rails/commands.rb:30
railties (3.0.8) lib/rails/commands.rb:27:in `tap'
railties (3.0.8) lib/rails/commands.rb:27
script/rails:6:in `require'
script/rails:6
Here is the relevant code from lib/active_support/callbacks.rb, which I modified to print out the method arguments
def run_callbacks(kind, *args, &block)
p *args
p &block
p kind
send("_run_#{kind}_callbacks", *args, &block)
end
Here is the output with the above code:
"callback arguments"
:checkout
"callback arguments"
"index"
:process_action
Started GET "/images" for 127.0.0.1 at Sun Aug 28 06:40:37 +0200 2011
Processing by ImagesController#index as HTML Completed 500 Internal
Server Error in 1ms
ArgumentError (wrong number of arguments (2 for 0)):
Rendered
/home/jon/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(1.5ms) Rendered
/home/jon/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(8.6ms) Rendered
/home/jon/.rvm/gems/ruby-1.8.7-p334/gems/actionpack-3.0.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb
within rescues/layout (20.9ms)
Here is the DB schema:
create_table "images", :force => true do |t|
t.string "photo"
t.integer "w"
t.integer "h"
t.integer "eyes"
t.integer "head_top"
t.integer "head_bottom"
t.integer "user_id"
t.string "face_params", :limit => 2000
t.string "smiling"
t.string "facing"
t.string "filename"
t.string "content_type"
t.binary "data", :limit => 16777215
t.datetime "created_at"
t.datetime "updated_at"
end
Here is the relevant controller code:
def index
p "made it to index action--------------------------"
if user_signed_in?
#user = current_user
#selected_image = #user.get_selected_image
#problems = #selected_image ? #selected_image.problems.all : Array.new
#images = #user.images - Array[#selected_image]
#image = #user.image_build
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #images }
end
else
redirect_to root_path
end
end
All the other controllers seem to work. Other actions for this controller give the same error.
Any ideas?
EDIT: Added Routes.rb as per comment below
Id::Application.routes.draw do
resources :payment_notifications
resources :addresses
resources :orders do
member do
get "select_address_for"
put "set_address_for"
get "complete"
end
end
resources :prints do
member do
post "email"
get "confirm_email"
end
end
resources :images do
member do
put "select"
get "thumb"
get "send"
end
end
resources :docs do
member do
get "image_for"
end
end
devise_for :users
##This will redirect the user after sign-in or sign-up.
##Otherwise is will default to the 'root' route, defined below
#devise_for :users do
# get 'users', :to => 'docs#index', :as => :user_root # Rails 3
#end
resources :admins
resources :assistants
resources :books
resources :links
resources :categories
resources :tips
resources :rules
resources :countries
resources :types
resources :continents
# match ':controller(/:action(/:id(.:format)))'
# match ':controller/:action/:id'
# match ':controller/:action/:id.:format'
# match ':controller/:action.:format'
end
You're redefining the send method in your images controller, which is a base ruby method. Rename it to something else and your problem should disappear.