Does Kaminari Work with Inherited Resources? - activerecord

Does anyone know whether the Kaminari gem works with Inherited Resources, because I'm trying to keep my code dry and don't want to repeat myself?
What the plan is, is to render 3 different tables i.e. an Errors table, Messages table and Subscribers table using one partial. However because I'm using Inherited Resources I need all of the code that I'd use in the errors controller, messages controller and subscribers controller, to come through the Admin Application Controller.
I hope this makes sense to anyone reading this
Here's my code:
Admin Application Controller
class Admin::ApplicationController < InheritedResources::Base
protect_from_forgery
include ResourcesHelper
layout "admin"
#Setup
before_filter :set_resource_variable
before_filter :set_pagination_variable, only: :index
#Authentication
skip_before_filter :authenticate_user!
before_filter :authenticate_admin!
#Authorization
skip_before_filter :check_authentication
#Index
#Custom Index For Application/Index (no inheritance)
def index
#users = User.all
#attributes = %w(messages subscribers)
end
#Create
def create
create! { collection_path }
end
#Update
def update
update! { collection_path }
end
private
#Set Model Variable
def set_resource_variable
#resource = self.resource_class
#model = "#{#resource}".downcase.to_sym
end
#Strong Params
def permitted_params
attributes = attributes(#resource) + %w(user_id admin_id) + [image_pages_attributes: [:caption, image_attributes: [:image]]]
params.permit(#model => attributes)
end
end
Errors Controller
def collection
#errors ||= end_of_association_chain.page params[:page]
end
Table Partial
<h2><%= model %></h2>
<% unless collection.blank? %>
<table class="sort">
<thead>
<tr>
<% model.attribute_names.each do |attr| %>
<th><%= model.human_attribute_name(attr) %></th>
<% end %>
<th colspan="2"> </th>
<% if model.name == "Error" %><th> </th><% end %>
</tr>
</thead>
<tbody>
<%= render partial: "admin/resources/table/row", collection: collection, as: :resource, locals: {model: model} %>
</tbody>
</table>
Here is the stack trace.
This error was thrown due to a current_page error:
undefined method `current_page' for nil:NilClass
Stack Trace
kaminari (0.15.0) lib/kaminari/helpers/action_view_extension.rb:18:in `paginate'
app/views/admin/resources/_table.html.erb:18:in `_app_views_admin_resources__table_html_erb___69500511_4075212'
actionpack (4.0.2) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.2) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.2) lib/action_view/template.rb:141:in `render'
actionpack (4.0.2) lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
actionpack (4.0.2) lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
actionpack (4.0.2) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.2) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (4.0.2) lib/action_view/renderer/partial_renderer.rb:278:in `render'
actionpack (4.0.2) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionpack (4.0.2) lib/action_view/renderer/renderer.rb:21:in `render'
actionpack (4.0.2) lib/action_view/helpers/rendering_helper.rb:24:in `render'
app/views/admin/application/index.html.erb:14:in `block in _app_views_admin_application_index_html_erb__1045387382_50813388'
app/views/admin/application/index.html.erb:12:in `each'
app/views/admin/application/index.html.erb:12:in `_app_views_admin_application_index_html_erb__1045387382_50813388'
actionpack (4.0.2) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.2) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.2) lib/action_view/template.rb:141:in `render'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
actionpack (4.0.2) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.2) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:48:in `block in render_template'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:56:in `render_with_layout'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:47:in `render_template'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionpack (4.0.2) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionpack (4.0.2) lib/action_view/renderer/renderer.rb:23:in `render'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:127:in `_render_template'
actionpack (4.0.2) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:120:in `render_to_body'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:33:in `render_to_body'
actionpack (4.0.2) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:97:in `render'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.0.2) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
activesupport (4.0.2) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.0.2) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.2) lib/active_support/callbacks.rb:433:in `_run__894777034__process_action__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.2) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:680:in `call'
lib/error_alert.rb:14:in `call'
rack-cors (0.2.9) lib/rack/cors.rb:54:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.2) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.2) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.2) lib/active_support/callbacks.rb:373:in `_run__692376707__call__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.2) lib/rails/engine.rb:511:in `call'
railties (4.0.2) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
thin (1.6.1) lib/thin/connection.rb:82:in `block in pre_process'
thin (1.6.1) lib/thin/connection.rb:80:in `catch'
thin (1.6.1) lib/thin/connection.rb:80:in `pre_process'
thin (1.6.1) lib/thin/connection.rb:55:in `process'
thin (1.6.1) lib/thin/connection.rb:41:in `receive_data'
eventmachine-1.0.3-x86 (mingw32) lib/eventmachine.rb:187:in `run_machine'
eventmachine-1.0.3-x86 (mingw32) lib/eventmachine.rb:187:in `run'
thin (1.6.1) lib/thin/backends/base.rb:73:in `start'
thin (1.6.1) lib/thin/server.rb:162:in `start'
rack (1.5.2) lib/rack/handler/thin.rb:16:in `run'
rack (1.5.2) lib/rack/server.rb:264:in `start'
railties (4.0.2) lib/rails/commands/server.rb:84:in `start'
railties (4.0.2) lib/rails/commands.rb:76:in `block in <top (required)>'
railties (4.0.2) lib/rails/commands.rb:71:in `tap'
railties (4.0.2) lib/rails/commands.rb:71:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'

I think it does judging from this snippet from https://github.com/josevalim/inherited_resources
class ProjectsController < InheritedResources::Base
protected
def collection
#projects ||= end_of_association_chain.paginate(:page => params[:page])
end
end
So for Kaminari you might do this:
class ProjectsController < InheritedResources::Base
protected
def collection
#projects ||= end_of_association_chain.page params[:page]
end
end
Although I haven't had a chance to try it yet.
I have tested this and it works perfectly.

Related

Getting TypeError - can't clone Symbol: error when using active_model_serializer

So I have Ruby 2.1.3 with Rails 4.2.0 and I am using the active_model_serializer gem to send proper json data to AngularJs.
Not I only upgraded today from Rails 4.1.6 to 4.2.0. With the previous version the problem did not exist.
The problem appears when I try to get json from my medium show action:
def show
medium = Medium.find(params[:id])
respond_with medium, include: [{comments: :user}, :likes, :user]
end
And this is how the same action looked before I upgraded rails:
def show
medium = Medium.includes([{comments: :user}, :likes, :user]).find(params[:id])
respond_with medium
end
It used to be able to work the old way but with the new rails, it does not have the included ActiveModels with the medium anymore in the sent json data.
So I changed the code using the examples on this page: https://github.com/rails-api/active_model_serializers
Here is how my media serializer looks like:
class MediumSerializer < ActiveModel::Serializer
attributes :id,
:text,
:image_video_file_name,
:image_video_croppable_url,
:image_video_thumb_url,
:image_video_big_url
has_one :user
has_many :comments, :likes
end
Before the upgrade it looked like this:
class MediumSerializer < ActiveModel::Serializer
attributes :id,
:text,
:image_video_file_name,
:image_video_croppable_url,
:image_video_thumb_url,
:image_video_big_url
has_one :user
has_many :comments, :likes
def include_user?
object.association(:user).loaded?
end
def include_comments?
object.association(:comments).loaded?
end
def include_associations?
object.association(:user, :comments, :likes).loaded?
end
end
So, when making a query for that show action, the error that my Rails application throws is this:
Started GET "/media/7.json" for 127.0.0.1 at 2014-12-25 18:13:43 +0200
Processing by Content::MediaController#show as JSON
Parameters: {"id"=>"7"}
Medium Load (0.4ms) SELECT "media".* FROM "media" WHERE "media"."id" = $1 LIMIT 1 [["id", 7]]
Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."medium_id" = $1 [["medium_id", 7]]
Completed 500 Internal Server Error in 59ms
TypeError - can't clone Symbol:
activesupport (4.2.0) lib/active_support/core_ext/object/try.rb:77:in `try!'
activesupport (4.2.0) lib/active_support/core_ext/object/try.rb:63:in `try'
activerecord (4.2.0) lib/active_record/serialization.rb:12:in `serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:114:in `block (2 levels) in serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:114:in `block in serializable_hash'
activemodel (4.2.0) lib/active_model/serialization.rb:158:in `block in serializable_add_includes'
activemodel (4.2.0) lib/active_model/serialization.rb:156:in `serializable_add_includes'
activemodel (4.2.0) lib/active_model/serialization.rb:112:in `serializable_hash'
activerecord (4.2.0) lib/active_record/serialization.rb:17:in `serializable_hash'
activemodel (4.2.0) lib/active_model/serializers/json.rb:99:in `as_json'
app/models/medium.rb:32:in `as_json'
activesupport (4.2.0) lib/active_support/json/encoding.rb:34:in `encode'
activesupport (4.2.0) lib/active_support/json/encoding.rb:21:in `encode'
activesupport (4.2.0) lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:116:in `block in <module:Renderers>'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:45:in `block in _render_to_body_with_renderer'
/home/kaspar/.rbenv/versions/2.1.3/lib/ruby/2.1.0/set.rb:263:in `each'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:41:in `_render_to_body_with_renderer'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/kaspar/.rbenv/versions/2.1.3/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
responders (2.0.2) lib/action_controller/responder.rb:258:in `display'
responders (2.0.2) lib/action_controller/responder.rb:212:in `api_behavior'
responders (2.0.2) lib/action_controller/responder.rb:191:in `rescue in to_format'
responders (2.0.2) lib/action_controller/responder.rb:185:in `to_format'
responders (2.0.2) lib/action_controller/responder.rb:163:in `respond'
responders (2.0.2) lib/action_controller/responder.rb:156:in `call'
responders (2.0.2) lib/action_controller/respond_with.rb:203:in `respond_with'
app/controllers/content/media_controller.rb:17:in `show'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.0) lib/rack/etag.rb:24:in `call'
rack (1.6.0) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.0) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.0) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.0) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (2.1.0) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.0) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.0) lib/better_errors/middleware.rb:57:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.0) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.0) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.0) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.0) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
passenger (4.0.56) lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
passenger (4.0.56) lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
passenger (4.0.56) lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
passenger (4.0.56) lib/phusion_passenger/request_handler.rb:455:in `block (3 levels) in start_threads'
Worth noting maybe that if I remove both of the :user includes from the new include: method, then everything works. But I want each comment to also include the user that created it and medium to also include the user that uploaded it.
What could be the problem here? Let me know if some more info is needed.
EDIT: The problem seems to be only with the :user for comments:.
Posting my comments serializer here:
class CommentSerializer < ActiveModel::Serializer
attributes :id,
:text,
:user_id,
:medium_id
has_one :user
end
I encountered the same error using the built in ActiveModel serializer for JSON. You can overwrite a model's as_json method with similar syntax to your respond_with call. Using your models I had
include: [{comments: :user} ... ]
when what I needed was
include: [{comments: {include: [:user]}}, ... ]
See if the following change fixes your problem (if you're still having it).
Change
respond_with medium, include: [{comments: :user}, :likes, :user]
to
respond_with medium, include: [{comments: {include: [:user]}}, :likes, :user]

LoadError in Users::OmniauthCallbacksController#facebook - Rails 4

well, I am trying to code a system that uses facebook login. When I click on the facebook link it goes to facebook page for authentication and on the callback show me this error: Unable to autoload constant Users::OmniauthCallbacksController, expected path/app/controllers/users/omniauth_callbacks_controller.rb to define it.
I am using devise gem, omniauth gem, ruby 2 and rails 4.
My user_controller.rb
class User::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
#user = User.find_for_facebook_oauth(request.env['omniauth.auth'], current_user)
if #user.persisted?
sign_in_and_redirect #user, :event => :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, :kind => 'Facebook') if is_navigational_format?
else
session['devise.facebook_data'] = request.env['omniauth.auth']
redirect_to new_user_registration_url
end
end
end
My user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def self.find_for_facebook_oauth(auth, signed_in_resource = nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
if user
return user
else
registered_user = User.where(:email => auth.info.email).first
if registered_user
return registered_user
else
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20],
)
end
end
end
end
On routes.rb I have this line:
devise_for :users, :controllers => { :omniauth_callbacks => 'users/omniauth_callbacks' }
The Full Trace is this:
activesupport (4.0.0) lib/active_support/dependencies.rb:463:in `load_missing_constant'
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:228:in `const_get'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:228:in `block in constantize'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in `each'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in `inject'
activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in `constantize'
activesupport (4.0.0) lib/active_support/dependencies.rb:534:in `get'
activesupport (4.0.0) lib/active_support/dependencies.rb:565:in `constantize'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:76:in `controller_reference'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:66:in `controller'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:44:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/mapper.rb:44:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.1.4) lib/omniauth/strategy.rb:401:in `call_app!'
omniauth (1.1.4) lib/omniauth/strategy.rb:363:in `callback_phase'
omniauth-oauth2 (1.1.1) lib/omniauth/strategies/oauth2.rb:77:in `callback_phase'
omniauth (1.1.4) lib/omniauth/strategy.rb:226:in `callback_call'
omniauth (1.1.4) lib/omniauth/strategy.rb:182:in `call!'
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__878755641__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
C:/Development/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
C:/Development/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
C:/Development/Ruby200/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
So that's my problem I hope someone can help me.
EDIT: rather than what I implied to, my callbacks controller is called omniauth_callbacks_controller.rb.
1. As sevenseacat suggested I changed the reference from users to user on my route file, but the problem still happened.
2. As *uno_ordinary* suggested I added :omniauth_providers => :facebook on my model but after that I received a new error when starting the server:
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
C:3:in `rescue in execute_if_updated': Rails::Application::RoutesReloader#execute_if_updated delegated to updater.execute_if_updated, but updater is nil: #<Rails::Application::RoutesReloader:0x4375db8 #paths=["C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config/routes.rb"], #route_sets=[#<ActionDispatch::Routing::RouteSet:0x449da98>]> (RuntimeError)
from C:131071:in `execute_if_updated'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `instance_exec'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `run'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:55:in `block in run_initializers'
from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each'
from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from'
from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component'
from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:180:in `each'
from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component'
from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:54:in `run_initializers'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:215:in `initialize!'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config/environment.rb:5:in `<top (required)>'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config.ru:3:in `block in <main>'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config.ru:in `new'
from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config.ru:in `<main>'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/server.rb:48:in `app'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/server.rb:75:in `start'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:78:in `block in <top (required)>'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `tap'
from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
If your callbacks controller is in a file called user_controller.rb (as your post implies), this is incorrect.
To follow Rails' naming conventions, it needs to be in app/controllers/users/omniauth_callbacks_controller.rb (as the error tells you).
You also have a problem with namespacing - your route for the callbacks controller refers to users (plural), while your controller namespace is User (singular).
Try to add to User model the following:
:omniauthable, :omniauth_providers => [:facebook]

active admin called nil id

im working with active admin i have collection_select or dropdown box that populates the packages and crews im just wondering i why this
undefined method `[]' for nil:NilClass`
i dont know what happened but it run few days ago and tested it once but now it raises this error are there possible way to solve this?
def create
#package = Package.find(params[:package_crew][:package_id])
crew = Crew.find(params[:package_crew][:crew_id] )
#package_crew = #package.add_crew(crew.id)
if #package_crew.save
redirect_to {admin_package_crew_url}
end
end
thanks in advance more power to us all
i tried this
def create
#pid = params[:package_crew][:crew_id] if params[:package_crew]
pid,cid = 2 #tried nil,0..10
if params[:package_crew]
pid = params[:package_crew][:package_id]
cid = params[:package_crew][:crew_id]
end
#package = Package.find_by_id(pid)
crew = Crew.find_by_id(cid)
##package = Package.find(params[:package_crew][:package_id])
#crew = Crew.find(params[:package_crew][:crew_id] )
#package_crew = #package.add_crew(crew.id)
if #package_crew.save
redirect_to {admin_package_crew_url}
end
end
add_crew method
def add_crew(crew_id)
current_package = package_crews.find_by_crew_id(crew_id)
if current_package
current_package.quantity = current_package.quantity.to_i + 1
else
current_package = package_crews.build(:crew_id => crew_id)
current_package.price = current_package.crew.price
end
current_package
end
moving parameters
{"utf8"=>"✓",
"authenticity_token"=>"n4WZpQpES6mnSO12oGKV/MeoxAVvV9OmJQKfhcpnvQM=",
"crew_for_package"=>{"package_id"=>"1",
"crew_id"=>"1",
"quantity"=>"1"},
"commit"=>"Create Package crew"}
stack error
app/admin/package_crews.rb:35:in `create'
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.1) lib/active_support/callbacks.rb:447:in `_run__304549499__process_action__775442107__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.3) lib/journey/router.rb:68:in `block in call'
journey (1.0.3) lib/journey/router.rb:56:in `each'
journey (1.0.3) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
sass (3.1.15) lib/sass/plugin/rack.rb:54:in `call'
warden (1.1.1) lib/warden/manager.rb:35:in `block in call'
warden (1.1.1) lib/warden/manager.rb:34:in `catch'
warden (1.1.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:35: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__1039351831__call__833288071__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'
railties (3.2.1) lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_request_handler.rb:513:in `accept_and_process_next_request'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:206:in `start_request_handler'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:171:in `block in handle_spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/utils.rb:479:in `safe_fork'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:166:in `handle_spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:180:in `start'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:129:in `start'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
<internal:prelude>:10:in `synchronize'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/helper-scripts/passenger-spawn-server:99:in `<main>'
As i can understand,
your params[:package_crew] is returning nil
check your params[:package_crew]
try following
pid,cid = 0
if params[:package_crew]
pid = params[:package_crew][:package_id]
cid = params[:package_crew][:crew_id]
end
#package = Package.find(pid)
#crew = Crew.find(cid)
i would suggest rather use this.
Package.find_by_id(pid) # this wont raise exception, when record is not found
Crew.find_by_id(cid)

Called id for nil, Active admin

im working with active admin i have collection_select or dropdown box that populates the packages and crews im just wondering i why this
undefined method `[]' for nil:NilClass`
i dont know what happened but it run few days ago and tested it once but now it raises this error are there possible way to solve this?
def create
#package = Package.find(params[:package_crew][:package_id])
crew = Crew.find(params[:package_crew][:crew_id] )
#package_crew = #package.add_crew(crew.id)
if #package_crew.save
redirect_to {admin_package_crew_url}
end
end
thanks in advance more power to us all
i tried this and
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id
tried create
def create
#pid = params[:package_crew][:crew_id] if params[:package_crew]
pid,cid = 2 #tried nil,0..10
if params[:package_crew]
pid = params[:package_crew][:package_id]
cid = params[:package_crew][:crew_id]
end
#package = Package.find_by_id(pid)
crew = Crew.find_by_id(cid)
##package = Package.find(params[:package_crew][:package_id])
#crew = Crew.find(params[:package_crew][:crew_id] )
#package_crew = #package.add_crew(crew.id)
if #package_crew.save
redirect_to {admin_package_crew_url}
end
end
add_crew method
def add_crew(crew_id)
current_package = package_crews.find_by_crew_id(crew_id)
if current_package
current_package.quantity = current_package.quantity.to_i + 1
else
current_package = package_crews.build(:crew_id => crew_id)
current_package.price = current_package.crew.price
end
current_package
end
moving parameters
{"utf8"=>"✓",
"authenticity_token"=>"n4WZpQpES6mnSO12oGKV/MeoxAVvV9OmJQKfhcpnvQM=",
"crew_for_package"=>{"package_id"=>"1",
"crew_id"=>"1",
"quantity"=>"1"},
"commit"=>"Create Package crew"}
stack error
app/admin/package_crews.rb:35:in `create'
actionpack (3.2.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.1) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.1) lib/active_support/callbacks.rb:447:in `_run__304549499__process_action__775442107__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.3) lib/journey/router.rb:68:in `block in call'
journey (1.0.3) lib/journey/router.rb:56:in `each'
journey (1.0.3) lib/journey/router.rb:56:in `call'
actionpack (3.2.1) lib/action_dispatch/routing/route_set.rb:589:in `call'
sass (3.1.15) lib/sass/plugin/rack.rb:54:in `call'
warden (1.1.1) lib/warden/manager.rb:35:in `block in call'
warden (1.1.1) lib/warden/manager.rb:34:in `catch'
warden (1.1.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:35: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__1039351831__call__833288071__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'
railties (3.2.1) lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/request_handler.rb:96:in `process_request'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_request_handler.rb:513:in `accept_and_process_next_request'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_request_handler.rb:274:in `main_loop'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:206:in `start_request_handler'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:171:in `block in handle_spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/utils.rb:479:in `safe_fork'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:166:in `handle_spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:180:in `start'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/rack/application_spawner.rb:129:in `start'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
<internal:prelude>:10:in `synchronize'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
/home/led/.passenger/standalone/3.0.11-x86-ruby1.9.3-linux-gcc4.6.3-1002/support/helper-scripts/passenger-spawn-server:99:in `<main>'
What you send :
{
"crew_for_package" => {
"package_id"=>"1",
"crew_id"=>"1",
"quantity"=>"1"
}
}
What you call :
params[:package_crew][:package_id]
params[:package_crew][:crew_id]
You see something wrong? This might not fix your problem, but it should at least send you in the right direction! :-)

How to make this work in Rails 3?

I am updating a Rails project and found a piece of code that doesn't work in Rails 3 and Ruby 1.9.2 (it did in Rails 2.3 and Ruby 1.8.7) and looks quite messy anyway!
def speech_box(title, options = {}, &block)
block_to_partial 'layouts/speech_box', options.merge(:title => title), &block
end
def block_to_partial(partial_name, options = {}, &block)
options.merge!(:body => capture(&block))
concat(render(:partial => partial_name, :locals => options), block.binding)
end
I am getting the following error when I call:
<% speech_box("hello") do %>
<p>lorem ipsum</p>
<% end %>
wrong number of arguments (2 for 1)
How come this doesn't work in Rails 3?
As requested below, here is the trace:
actionpack (3.1.0) lib/action_view/helpers/text_helper.rb:51:in `concat'
app/helpers/application_helper.rb:277:in `block_to_partial'
app/helpers/application_helper.rb:272:in `speech_box'
app/views/talks/new.html.erb:46:in `_app_views_talks_new_html_erb__3484685084088947386_2193902320'
actionpack (3.1.0) lib/action_view/template.rb:144:in `block in render' activesupport (3.1.0)
lib/active_support/notifications.rb:55:in `instrument' actionpack (3.1.0)
lib/action_view/template.rb:142:in `render' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:40:in `block (2 levels) in render_template' actionpack (3.1.0)
lib/action_view/renderer/abstract_renderer.rb:33:in `block in instrument' 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_view/renderer/abstract_renderer.rb:33:in `instrument' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:39:in `block in render_template' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:47:in `render_with_layout' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:38:in `render_template' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:12:in `block in render' actionpack (3.1.0)
lib/action_view/renderer/abstract_renderer.rb:22:in `wrap_formats' actionpack (3.1.0)
lib/action_view/renderer/template_renderer.rb:9:in `render' actionpack (3.1.0)
lib/action_view/renderer/renderer.rb:36:in `render_template' actionpack (3.1.0)
lib/action_view/renderer/renderer.rb:17:in `render' actionpack (3.1.0)
lib/abstract_controller/rendering.rb:120:in `_render_template' actionpack (3.1.0)
lib/action_controller/metal/streaming.rb:250:in `_render_template' actionpack (3.1.0)
lib/abstract_controller/rendering.rb:114:in `render_to_body' actionpack (3.1.0)
lib/action_controller/metal/renderers.rb:30:in `render_to_body' actionpack (3.1.0)
lib/action_controller/metal/compatibility.rb:43:in `render_to_body' actionpack (3.1.0)
lib/abstract_controller/rendering.rb:99:in `render' actionpack (3.1.0)
lib/action_controller/metal/rendering.rb:16:in `render' actionpack (3.1.0)
lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render' activesupport (3.1.0)
lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
.............
concat is gone. Simply remove concat and make sure to call speech_box using <%= instead of <%.
def block_to_partial(partial_name, options = {}, &block)
options.merge!(:body => capture(&block))
render(:partial => partial_name, :locals => options)
end
<%= speech_box("hello") do %>
<p>lorem ipsum</p>
<% end %>

Resources