will_paginate mistaking a relation for an array - ruby-on-rails

Rails 5 application with will_paginate has the following log entry:
Usercontent Load (0.7ms) SELECT "usercontents".* FROM "usercontents" WHERE (id IN (171,172,33,34,35,156,173,144,78,81,88,90,93,96,36,37,38,48,87,89,94,39,175,176) AND contenttype_id = 1)
↳ app/controllers/travels_controller.rb:45
and the controller action specifies
#points = Usercontent.where('id IN (?) AND contenttype_id = ?', #all_valid_usercontents, 1).paginate(page: params[:page], per_page: 18)
if I run in the console that line as
#points = Usercontent.where('id IN (?) AND contenttype_id = ?', [171,172,33,34,35,156,173,144,78,81,88,90,93,96,36,37,38,48,87,89,94,39,175,176], 1).paginate(page: 1, per_page: 18)
then
> #points.class
=> Usercontent::ActiveRecord_Relation
Inserting in the controller debugging points:
Rails.logger.info #a_points.class
Rails.logger.info #points.class
returns
Usercontent::ActiveRecord_Relation
Usercontent::ActiveRecord_Relation
View
<div id='yield_pcontent'>
<%= render 'points' %>
</div>
<div id="infinite-scrolling">
<%= will_paginate %>
</div>
partial _points.html.erb
<div id=points class='tableize'>
<%= render partial: 'point', collection: #points %>
</div>
How can will_paginate thus recognize helper <%= will_paginate %> as an array
undefined method `total_pages' for #<Array:0x00007f9a27d47718>
note: adding require 'will_paginate/array' does not change this behaviour
update
activerecord (5.2.3) lib/active_record/relation/delegation.rb:125:in `method_missing'
will_paginate (3.1.7) lib/will_paginate/view_helpers.rb:73:in `will_paginate'
will_paginate (3.1.7) lib/will_paginate/view_helpers/action_view.rb:33:in `will_paginate'
app/views/travels/_region_points.html.erb:5:in `_app_views_travels__region_points_html_erb__2375899185333558082_70150028432340'
actionview (5.2.3) lib/action_view/template.rb:159:in `block in render'
activesupport (5.2.3) lib/active_support/notifications.rb:170:in `instrument'
actionview (5.2.3) lib/action_view/template.rb:354:in `instrument_render_template'
actionview (5.2.3) lib/action_view/template.rb:157:in `render'
actionview (5.2.3) lib/action_view/renderer/partial_renderer.rb:344:in `block in render_partial'
actionview (5.2.3) lib/action_view/renderer/abstract_renderer.rb:44:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.2.3) lib/action_view/renderer/abstract_renderer.rb:43:in `instrument'
actionview (5.2.3) lib/action_view/renderer/partial_renderer.rb:333:in `render_partial'
actionview (5.2.3) lib/action_view/renderer/partial_renderer.rb:312:in `render'
actionview (5.2.3) lib/action_view/renderer/renderer.rb:49:in `render_partial'
actionview (5.2.3) lib/action_view/helpers/rendering_helper.rb:36:in `render'
app/views/travels/cc.html.erb:33:in `_app_views_travels_cc_html_erb___2132430413937746159_70149902220280'
actionview (5.2.3) lib/action_view/template.rb:159:in `block in render'
activesupport (5.2.3) lib/active_support/notifications.rb:170:in `instrument'
actionview (5.2.3) lib/action_view/template.rb:354:in `instrument_render_template'
actionview (5.2.3) lib/action_view/template.rb:157:in `render'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (5.2.3) lib/action_view/renderer/abstract_renderer.rb:44:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.2.3) lib/action_view/renderer/abstract_renderer.rb:43:in `instrument'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:16:in `render'
actionview (5.2.3) lib/action_view/renderer/renderer.rb:44:in `render_template'
actionview (5.2.3) lib/action_view/renderer/renderer.rb:25:in `render'
actionview (5.2.3) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.2.3) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionview (5.2.3) lib/action_view/rendering.rb:84:in `render_to_body'
actionpack (5.2.3) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.2.3) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (5.2.3) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (5.2.3) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
activesupport (5.2.3) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/home/jerdvo/.rbenv/versions/2.6.1/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (5.2.3) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.2.3) lib/active_record/railties/controller_runtime.rb:31:in `cleanup_view_runtime'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:45:in `render'
wicked_pdf (1.2.2) lib/wicked_pdf/pdf_helper.rb:46:in `call'
wicked_pdf (1.2.2) lib/wicked_pdf/pdf_helper.rb:46:in `render_with_wicked_pdf'
wicked_pdf (1.2.2) lib/wicked_pdf/pdf_helper.rb:30:in `render'
app/controllers/travels_controller.rb:59:in `cc'
actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (5.2.3) lib/abstract_controller/base.rb:194:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.2.3) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (5.2.3) lib/active_support/callbacks.rb:132:in `run_callbacks'
actionpack (5.2.3) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
activerecord (5.2.3) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
actionpack (5.2.3) lib/abstract_controller/base.rb:134:in `process'
actionview (5.2.3) lib/action_view/rendering.rb:32:in `process'
actionpack (5.2.3) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (5.2.3) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:34:in `serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:840:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.0.7) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.0.7) lib/rack/etag.rb:25:in `call'
rack (2.0.7) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.7) lib/rack/head.rb:12:in `call'
actionpack (5.2.3) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.7) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.7) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/cookies.rb:670:in `call'
activerecord (5.2.3) lib/active_record/migration.rb:559:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.3) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:61: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:22: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 (5.2.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
request_store (1.4.1) lib/request_store/middleware.rb:19:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/method_override.rb:22:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
railties (5.2.3) lib/rails/engine.rb:524:in `call'
/usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:97:in `process_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:149:in `accept_and_process_next_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
/usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:113:in `block in create_thread_and_abort_on_exception'
Update 2
based on a suggestion, two behaviours are being observed:
<%= will_paginate %> => undefined method `total_pages' for #<Array:0x00007f9a261e2540>
<%= will_paginate #points %> => undefined method `total_pages' for #<Usercontent::ActiveRecord_Relation:0x00007f9a26d07ab0>

He is waiting for an Object, not an Array. I share my solution, adapt it to your case.
def render_partial_resource(resources)
if resources.present?
if resources.respond_to?(:model_name)
folder = resources.model_name.name.pluralize.downcase
partial = resources.model_name.name.downcase
else
folder = resources.object.name.pluralize.downcase
partial = resources.object.name.downcase
end
resources.map do |resource|
render partial:"#{folder}/#{partial}", locals:{"#{partial}": resource}
end.join(" ").html_safe
end
end

Related

administrate gem customizing labels used for resources in dashboard collections

The administrate gem guide indicates the labels for ressources can be adapted to attributes of that model
def display_resource(user)
user.name
end
However this does not appear to function on relations
Card belongs_to User and the goal is to represent the ressource user in the CardDashboard with
card.user.last_name + ', ' + card.user.first_name
However, even a succinct definition
def display_resource(user)
card.user.last_name
end
leads to undefined local variable or method 'card' for #<CardDashboard
It appears the display_resource definition is only applicable to its own class as
def display_resource(user)
user.last_name + ', ' + user.first_name
end
will work.
Is there a mechanism by which the resource label can be defined on a per-class basis using nested attributes ?
update error stack
app/dashboards/user_dashboard.rb:212:in `display_resource'
app/views/admin/cards/_form.html.erb:38
app/views/admin/cards/_form.html.erb:36:in `each'
app/views/admin/cards/_form.html.erb:36
app/views/admin/cards/_form.html.erb:17
app/views/admin/cards/edit.html.erb:35
update
Full stack trace
administrate (0.14.0) lib/administrate/base_dashboard.rb:39:in `block in attribute_type_for'
administrate (0.14.0) lib/administrate/base_dashboard.rb:38:in `fetch'
administrate (0.14.0) lib/administrate/base_dashboard.rb:38:in `attribute_type_for'
administrate (0.14.0) lib/administrate/page/base.rb:30:in `attribute_field'
administrate (0.14.0) lib/administrate/page/form.rb:15:in `block in attributes'
administrate (0.14.0) lib/administrate/page/form.rb:14:in `map'
administrate (0.14.0) lib/administrate/page/form.rb:14:in `attributes'
app/views/admin/cards/_form.html.erb:36
actionview (6.0.3.4) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (6.0.3.4) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (6.0.3.4) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (6.0.3.4) lib/action_view/helpers/form_helper.rb:452:in `form_for'
client_side_validations (17.2.0) lib/client_side_validations/action_view/form_helper.rb:11:in `form_for'
app/views/admin/cards/_form.html.erb:17
actionview (6.0.3.4) lib/action_view/base.rb:274:in `_run'
actionview (6.0.3.4) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.3.4) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.3.4) lib/action_view/template.rb:385:in `instrument_render_template'
actionview (6.0.3.4) lib/action_view/template.rb:183:in `render'
actionview (6.0.3.4) lib/action_view/renderer/partial_renderer.rb:357:in `block in render_partial'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:88:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:87:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/partial_renderer.rb:346:in `render_partial'
actionview (6.0.3.4) lib/action_view/renderer/partial_renderer.rb:317:in `render'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:65:in `render_partial_to_object'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:53:in `render_partial'
actionview (6.0.3.4) lib/action_view/helpers/rendering_helper.rb:38:in `render'
app/views/admin/cards/edit.html.erb:35
actionview (6.0.3.4) lib/action_view/base.rb:274:in `_run'
actionview (6.0.3.4) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.3.4) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.3.4) lib/action_view/template.rb:385:in `instrument_render_template'
actionview (6.0.3.4) lib/action_view/template.rb:183:in `render'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:58:in `block (2 levels) in render_template'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:88:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/abstract_renderer.rb:87:in `instrument'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:57:in `block in render_template'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:65:in `render_with_layout'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:56:in `render_template'
actionview (6.0.3.4) lib/action_view/renderer/template_renderer.rb:13:in `render'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.0.3.4) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.0.3.4) lib/action_view/rendering.rb:117:in `block in _render_template'
actionview (6.0.3.4) lib/action_view/base.rb:304:in `in_rendering_context'
actionview (6.0.3.4) lib/action_view/rendering.rb:116:in `_render_template'
actionpack (6.0.3.4) lib/action_controller/metal/streaming.rb:218:in `_render_template'
actionview (6.0.3.4) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.0.3.4) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.0.3.4) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.0.3.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.0.3.4) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (6.0.3.4) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/home/uelcom/.rbenv/versions/2.6.1/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (6.0.3.4) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (6.0.3.4) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:43:in `render'
administrate (0.14.0) app/controllers/administrate/application_controller.rb:38:in `edit'
actionpack (6.0.3.4) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.3.4) lib/abstract_controller/base.rb:195:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.3.4) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.0.3.4) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (6.0.3.4) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.4) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.4) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.3.4) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.3.4) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.3.4) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.3.4) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.3.4) lib/action_view/rendering.rb:39:in `process'
actionpack (6.0.3.4) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (6.0.3.4) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.3.4) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.3.4) lib/action_dispatch/routing/route_set.rb:834:in `call'
warden (1.2.9) lib/warden/manager.rb:36:in `block in call'
warden (1.2.9) lib/warden/manager.rb:34:in `catch'
warden (1.2.9) lib/warden/manager.rb:34:in `call'
rack (2.2.3) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.3) lib/rack/etag.rb:27:in `call'
rack (2.2.3) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.3) lib/rack/head.rb:12:in `call'
actionpack (6.0.3.4) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.3) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.3) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.3.4) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.3.4) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.3.4) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:19:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.3.4) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.0.3.4) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.3.4) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.3.4) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.3.4) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.3.4) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rails_same_site_cookie (0.1.8) lib/rails_same_site_cookie/middleware.rb:13:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.3.4) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
webpacker (4.3.0) 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.3.4) lib/rails/engine.rb:527:in `call'
/usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:107:in `process_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:149:in `accept_and_process_next_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
/usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:113:in `block in create_thread_and_abort_on_exception'
Update_Dashboards
require "administrate/base_dashboard"
class CardDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
user: Field::BelongsTo.with_options(
searchable: true,
searchable_fields: ['first_name', 'last_name', 'email', 'mobile']
),
id: Field::Number,
virtual: Field::Boolean,
blocked: Field::Boolean,
code: Field::String,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze
COLLECTION_ATTRIBUTES = %i[
user
id
virtual
blocked
].freeze
SHOW_PAGE_ATTRIBUTES = %i[
user
id
virtual
blocked
code
created_at
updated_at
].freeze
FORM_ATTRIBUTES = %i[
user
virtual
blocked
code
].freeze
COLLECTION_FILTERS = {}.freeze
def display_resource(card)
card.user.last_name + ', ' + card.user.first_name
end
end
require "administrate/base_dashboard"
class UserDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
nation: Field::BelongsTo,
card: Field::HasOne,
notifications: Field::HasMany,
attachments: Field::HasMany,
id: Field::Number,
last_user_update: Field::Number,
last_user_update_at: Field::DateTime,
created_at: Field::DateTime,
updated_at: Field::DateTime,
}.freeze
COLLECTION_ATTRIBUTES = %i[
id
email
mobile
login_name
municipal
newsletter
].freeze
SHOW_PAGE_ATTRIBUTES = %i[
nation
card
notifications
attachments
id
email
first_name
last_name
date_of_birth
address
storage_bin
created_at
updated_at
].freeze
FORM_ATTRIBUTES = %i[
nation
card
notifications
attachments
email
mobile
].freeze
COLLECTION_FILTERS = {}.freeze
def display_resource(user)
user ? user.last_name + ', ' + user.first_name : "No user selected"
end
end
You say that you want to represent cards in the CardDashboard as follows:
card.user.last_name + ', ' + card.user.first_name
Then you use this code example, which is not working:
def display_resource(user)
card.user.last_name
end
I'm probably missing some info, and your example might have a typo, but what I would expect you to have is the following instead:
def display_resource(card)
card.user.last_name
end
The method CardDashboard#display_resource will receive cards as arguments, not users. You can't refer to card in your example because it's not defined (your argument is user instead). My example however should work, although it will represent the card as first_name+last_name everywhere, not just when represented as associations in other dashboards.

undefined method `[]' for nil:NilClass when trying to use API

Im new to ruby and I am trying to consume an api and display the data that I get from it but I keep on getting this error : undefined method `[]' for nil:NilClass. It's probably a stupid error but again i am new and cant figure it out. Here's my controller:
class RecipeController < ApplicationController
def Recipe
end
def search
recipe = find_recipe(params[:targetCalories])
unless recipe
flash[:alert] = 'Recipe not found'
return render action: :Recipe
end
#recipe = recipe.first
end
private
def request_api(url)
response = Excon.get(
url,
headers: {
'X-RapidAPI-Host' => URI.parse(url).host,
'X-RapidAPI-Key' => ENV.fetch('RAPIDAPI_API_KEY')
}
)
return nil if response.status != 200
JSON.parse(response.body)
end
def find_recipe(name)
request_api(
"https://spoonacular-recipe-food-nutrition-v1.p.rapidapi.com/recipes/mealplans/generate/#{URI.encode(targetCalories)}"
)
end
end
And here's my html:
<div class="container">
<div class="row">
<div class="col-lg-12 mt-5">
<% if flash[:alert] %>
<div class="alert alert-warning"><%= flash[:alert] %></div>
<% end %>
<div class="mx-auto mt-5" style="width: 400px">
<%= form_with(url: search_path, method: 'get', local: true) do %>
<div class="form-group">
<%= label_tag :country, 'Type amount of calories '%>
<%= text_field_tag :country, nil, placeholder: 'Eg. 1000', class: 'form-control' %>
</div>
<%= button_tag 'Search', class: 'btn btn-success btn-block' %>
<% end %>
</div>
</div>
</div>
<div class="container">
<div class="row mt-5">
<div class="col-lg-6">
<h4>Recipe info</h4>
<dl>
<dt>Day</dt>
<dd><%= #recipe['day'] %></dd>
<dt>Capital</dt>
<dd><%= #recipe['type'] %></dd>
<dt>Currency</dt>
<dd><%= #recipe['value'].join(', ') %></dd>
</dl>
</div>
</div>
</div>
Edit: this is the full stack trace
app/views/recipe/Recipe.html.erb:26
actionview (6.0.2.1) lib/action_view/base.rb:274:in `_run'
actionview (6.0.2.1) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.2.1) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.2.1) lib/action_view/template.rb:386:in `instrument_render_template'
actionview (6.0.2.1) lib/action_view/template.rb:183:in `render'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:59:in `block (2 levels) in render_template'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:89:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/abstract_renderer.rb:88:in `instrument'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:58:in `block in render_template'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:66:in `render_with_layout'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:57:in `render_template'
actionview (6.0.2.1) lib/action_view/renderer/template_renderer.rb:13:in `render'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.0.2.1) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.0.2.1) lib/action_view/rendering.rb:118:in `block in _render_template'
actionview (6.0.2.1) lib/action_view/base.rb:304:in `in_rendering_context'
actionview (6.0.2.1) lib/action_view/rendering.rb:117:in `_render_template'
actionpack (6.0.2.1) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionview (6.0.2.1) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.0.2.1) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.0.2.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
C:/Ruby26-x64/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (6.0.2.1) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:85:in `cleanup_view_runtime'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:43:in `render'
remotipart (1.4.4) lib/remotipart/render_overrides.rb:23:in `render'
actionpack (6.0.2.1) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.0.2.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:196:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (6.0.2.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.2.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.2.1) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.2.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.2.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.2.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.2.1) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.2.1) lib/action_view/rendering.rb:39:in `process'
actionpack (6.0.2.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (6.0.2.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.2.1) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.2.1) lib/action_dispatch/routing/route_set.rb:837:in `call'
rack-pjax (1.1.0) lib/rack/pjax.rb:12:in `call'
remotipart (1.4.4) lib/remotipart/middleware.rb:32:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.2.2) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.2) lib/rack/etag.rb:27:in `call'
rack (2.2.2) lib/rack/conditional_get.rb:27:in `call'
rack (2.2.2) lib/rack/head.rb:12:in `call'
actionpack (6.0.2.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.2) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.2) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.2.1) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.2.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.2.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.2.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.2.1) 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.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.2) lib/rack/method_override.rb:24:in `call'
rack (2.2.2) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.2.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.2) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.2.1) 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.1) lib/rails/engine.rb:526:in `call'
puma (4.3.1) lib/puma/configuration.rb:228:in `call'
puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
puma (4.3.1) lib/puma/server.rb:472:in `process_client'
puma (4.3.1) lib/puma/server.rb:328:in `block in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
undefined method `[]' for nil:NilClass on
app/views/recipe/Recipe.html.erb:26
Your #recipe instance variable is nil. Make sure you assign a value to it in your controller, in your def Recipe action.
PS: Controller actions should be lowercase, so def recipe.

Issue Passing parameters from erb template to controller in Rails

I am trying to call a method in view template from its respective controller which is acting weirdly based on different conditions.
PLease note I have tried this after getting failure on this
Triply nested model's form in rails showing error
First of all the controller method is defined as ->
helper_method :find_feature
def find_feature(fid)
#project.features.find(fid)
end
And the view file code where this method is called ->
<% #project.features.each do |fet| %>
<%= "#{fet.name} #{fet.id} " %> #It does work!!
<%= render partial: "taskform",locals: { fetr: find_feature(fet.id) } %>
<% end %>
This throws error as Couldn't find Feature without an ID
in the screenshot islt is shown params[:id] is used that is not right sorry for that I have used the method which is written above as find_feature(fid) very sorry I will update the screenshot once I get to my mqchine
**
the error shows Couldn't find a feature without an id when called by find_feature(fet.id)
**
BUt when I explicitly test with an id, manually, like->
<%= render partial: "taskform", locals: { fetr: find_feature(7) } %>
This works !
Full stack trace-
activerecord (5.2.3) lib/active_record/relation/finder_methods.rb:433:in `find_with_ids'
activerecord (5.2.3) lib/active_record/relation/finder_methods.rb:69:in `find'
activerecord (5.2.3) lib/active_record/associations/collection_association.rb:100:in `find'
activerecord (5.2.3) lib/active_record/associations/collection_proxy.rb:140:in `find'
app/controllers/projects_controller.rb:7:in `find_feature'
actionpack (5.2.3) lib/abstract_controller/helpers.rb:67:in `find_feature'
app/views/projects/show.html.erb:66:in `block in _app_views_projects_show_html_erb__3383934460746758134_70317115637280'
activerecord (5.2.3) lib/active_record/relation/delegation.rb:71:in `each'
activerecord (5.2.3) lib/active_record/relation/delegation.rb:71:in `each'
app/views/projects/show.html.erb:56:in `_app_views_projects_show_html_erb__3383934460746758134_70317115637280'
actionview (5.2.3) lib/action_view/template.rb:159:in `block in render'
activesupport (5.2.3) lib/active_support/notifications.rb:170:in `instrument'
actionview (5.2.3) lib/action_view/template.rb:354:in `instrument_render_template'
actionview (5.2.3) lib/action_view/template.rb:157:in `render'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (5.2.3) lib/action_view/renderer/abstract_renderer.rb:44:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.2.3) lib/action_view/renderer/abstract_renderer.rb:43:in `instrument'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (5.2.3) lib/action_view/renderer/template_renderer.rb:16:in `render'
actionview (5.2.3) lib/action_view/renderer/renderer.rb:44:in `render_template'
actionview (5.2.3) lib/action_view/renderer/renderer.rb:25:in `render'
actionview (5.2.3) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.2.3) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionview (5.2.3) lib/action_view/rendering.rb:84:in `render_to_body'
actionpack (5.2.3) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.2.3) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (5.2.3) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (5.2.3) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
activesupport (5.2.3) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/home/ayan/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (5.2.3) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.2.3) lib/active_record/railties/controller_runtime.rb:31:in `cleanup_view_runtime'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:45:in `render'
actionpack (5.2.3) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (5.2.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (5.2.3) lib/abstract_controller/base.rb:194:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.2.3) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (5.2.3) lib/active_support/callbacks.rb:132:in `run_callbacks'
actionpack (5.2.3) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.3) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.3) lib/active_support/notifications.rb:168:in `instrument'
actionpack (5.2.3) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (5.2.3) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
activerecord (5.2.3) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
actionpack (5.2.3) lib/abstract_controller/base.rb:134:in `process'
actionview (5.2.3) lib/action_view/rendering.rb:32:in `process'
actionpack (5.2.3) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (5.2.3) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:34:in `serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.3) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.3) lib/action_dispatch/routing/route_set.rb:840:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/builder.rb:64:in `call'
rack (2.0.7) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.0.7) lib/rack/etag.rb:25:in `call'
rack (2.0.7) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.7) lib/rack/head.rb:12:in `call'
actionpack (5.2.3) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.7) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.7) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/cookies.rb:670:in `call'
activerecord (5.2.3) lib/active_record/migration.rb:559:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.3) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.3) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:61: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 (5.2.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/method_override.rb:22:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
railties (5.2.3) lib/rails/engine.rb:524:in `call'
puma (3.12.1) lib/puma/configuration.rb:227:in `call'
puma (3.12.1) lib/puma/server.rb:660:in `handle_request'
puma (3.12.1) lib/puma/server.rb:474:in `process_client'
puma (3.12.1) lib/puma/server.rb:334:in `block in run'
puma (3.12.1) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
Request
Parameters:
{"id"=>"4"}
Can anyone suggests what is the reason of this behaviour ?
You are calling the find_feature with this code find_feature(fet.id), so you want to pass the id as a parameter.
Define the helper_method this way then:
def find_feature(fid)
#project.features.find(fid)
end
params[:fid] will look for a request parameter with the key fid in the request's params hash, you are passing a specific value to the method when you call it.
EDIT: Anyway, I'm not sure why are you doing that, you already have the feature object on the fet variable inside the loop, just do this:
<%= render partial: "taskform",locals: { fetr: fet } %>
error shows method calling with parmas[:fid], but you defined method as find_feature(fid) for finding feature , so there must be other method which is overriding , try it by giving different name like find_project_features it will work

Rails i18n and EasyTranslate

I am using a locale :cn which is not recognized by EasyTranslate. I am trying to create the if in the controller such as:
def index
#events = Events.all
if I18n.locale == :cn
#locale = ":zh-CN"
else
#locale = I18n.locale
end
end
so that I can use the following in my view:
<%= check_box_tag "by_cities[]", city %> <%= EasyTranslate.translate(city, :to => #locale, :key => #key) %>
BUt I get an EasyTranslate:EasyTranslateException in Event#index Invalid Value
How can i get EasyTranslate to take the new locale just for this method please?
Thank you
Et
Application Trace | Framework Trace | Full Trace
easy_translate (0.5.1) lib/easy_translate/request.rb:47:in `perform_raw'
easy_translate (0.5.1) lib/easy_translate/translation.rb:34:in `request_translations'
easy_translate (0.5.1) lib/easy_translate/threadable.rb:24:in `block in threaded_process'
easy_translate (0.5.1) lib/easy_translate/threadable.rb:24:in `map'
easy_translate (0.5.1) lib/easy_translate/threadable.rb:24:in `threaded_process'
easy_translate (0.5.1) lib/easy_translate/translation.rb:20:in `translate'
app/views/events/index.html.erb:21:in `block (2 levels) in _app_views_events_index_html_erb___49110152348777287_70221206769120'
app/views/events/index.html.erb:19:in `each'
app/views/events/index.html.erb:19:in `block in _app_views_events_index_html_erb___49110152348777287_70221206769120'
actionview (5.1.6) lib/action_view/helpers/capture_helper.rb:39:in `block in capture'
actionview (5.1.6) lib/action_view/helpers/capture_helper.rb:203:in `with_output_buffer'
actionview (5.1.6) lib/action_view/helpers/capture_helper.rb:39:in `capture'
actionview (5.1.6) lib/action_view/helpers/form_tag_helper.rb:70:in `form_tag'
app/views/events/index.html.erb:15:in `_app_views_events_index_html_erb___49110152348777287_70221206769120'
actionview (5.1.6) lib/action_view/template.rb:157:in `block in render'
activesupport (5.1.6) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.1.6) lib/action_view/template.rb:352:in `instrument_render_template'
actionview (5.1.6) lib/action_view/template.rb:155:in `render'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:52:in `block (2 levels) in render_template'
actionview (5.1.6) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.1.6) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:51:in `block in render_template'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:59:in `render_with_layout'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:50:in `render_template'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (5.1.6) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (5.1.6) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (5.1.6) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.1.6) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (5.1.6) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.1.6) lib/action_controller/metal/renderers.rb:141:in `render_to_body'
actionpack (5.1.6) lib/abstract_controller/rendering.rb:24:in `render'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (5.1.6) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/etienne/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
activesupport (5.1.6) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
searchkick (3.1.2) lib/searchkick/logging.rb:214:in `cleanup_view_runtime'
activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:29:in `cleanup_view_runtime'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:43:in `render'
wicked_pdf (1.1.0) lib/wicked_pdf/pdf_helper.rb:42:in `render_with_wicked_pdf'
wicked_pdf (1.1.0) lib/wicked_pdf/pdf_helper.rb:27:in `render'
actionpack (5.1.6) lib/action_controller/metal/implicit_render.rb:33:in `default_render'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
searchkick (3.1.2) lib/searchkick/logging.rb:209:in `process_action'
activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.6) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.6) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.0.6) lib/rack/etag.rb:25:in `call'
rack (2.0.6) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.6) lib/rack/head.rb:12:in `call'
rack (2.0.6) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.6) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.6) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
rollbar (2.18.0) lib/rollbar/middleware/rails/rollbar.rb:24:in `block in call'
rollbar (2.18.0) lib/rollbar.rb:146:in `scoped'
rollbar (2.18.0) lib/rollbar/middleware/rails/rollbar.rb:22:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
rollbar (2.18.0) lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:22: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 (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
request_store (1.4.1) lib/request_store/middleware.rb:19:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
railties (5.1.6) lib/rails/engine.rb:522:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'
Very simple actually :
In the controller :
if I18n.locale == :cn
#locale ="zh-CN"
else
#locale = (":"+ (I18n.locale).to_s)
end
In the view:
<%= check_box_tag "by_cities[]", city %> <%= EasyTranslate.translate(city, :to => #locale, :key => #key) %>

rails enum, load enum values based on condition is it possible?

lets say i have a model with enum like this :
class Apt < ActiveRecord::Base
enum apt_status: [ :draft, :publish, :unpublish, :waiting ]
end
and in my controller i load my enum like this :
#apt_statuses = Apt.apt_statuses
it's working fine, but what if i want to load my enum values based on user role?
so lets say if i am an admin i want to load all my enum values, and if i am not an admin, i only display 3 enum values
":draft, :unpublish, :waiting"
is it possible? please suggest. many thanks.
btw here is my controller, i really have no idea how to fill my
#apt_statuses
in else condition
ishaveprivilage = Usermaster.joins(:rolemasters, :rolemasters).where(id: #current_user.id, rolemasters: {name: "Super Admin"})
if ishaveprivilage
#apt_statuses = Apt.apt_statuses
else
end
i use my enum in view like this :
<%= f.select :apt_status, options_for_select(#apt_statuses.collect { |s| [s[0].humanize, s[0]] }, selected: #apt.apt_status), {} , class: "form-control" %>
error trace :
NoMethodError in Admin::Apts#new
Showing C:/Users/lenovo/Documents/urbanace/urbanacecode/app/views/admin/apts/_form.html.erb where line #134 raised:
undefined method `humanize' for :draft:Symbol
Trace of template inclusion: app/views/admin/apts/new.html.erb
Rails.root: C:/Users/lenovo/Documents/urbanace/urbanacecode
Application Trace | Framework Trace | Full Trace
app/views/admin/apts/_form.html.erb:134:in `block (2 levels) in _app_views_admin_apts__form_html_erb___8345113_90629556'
app/views/admin/apts/_form.html.erb:134:in `each'
app/views/admin/apts/_form.html.erb:134:in `collect'
app/views/admin/apts/_form.html.erb:134:in `block in _app_views_admin_apts__form_html_erb___8345113_90629556'
actionview (4.2.7) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.7) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
actionview (4.2.7) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionview (4.2.7) lib/action_view/helpers/form_helper.rb:444:in `form_for'
app/views/admin/apts/_form.html.erb:57:in `_app_views_admin_apts__form_html_erb___8345113_90629556'
actionview (4.2.7) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.7) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:143:in `render'
actionview (4.2.7) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
actionview (4.2.7) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.7) lib/action_view/renderer/partial_renderer.rb:309:in `render'
actionview (4.2.7) lib/action_view/renderer/renderer.rb:51:in `render_partial'
actionview (4.2.7) lib/action_view/helpers/rendering_helper.rb:35:in `render'
app/views/admin/apts/new.html.erb:2:in `_app_views_admin_apts_new_html_erb__809278863_96977256'
actionview (4.2.7) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.7) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.7) lib/action_view/template.rb:143:in `render'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.7) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.7) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.7) lib/action_view/renderer/renderer.rb:46:in `render_template'
actionview (4.2.7) lib/action_view/renderer/renderer.rb:27:in `render'
actionview (4.2.7) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.7) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.7) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.7) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.7) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.7) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.7) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.7) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.7) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.7) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:43:in `render'
remotipart (1.2.1) lib/remotipart/render_overrides.rb:14:in `render_with_remotipart'
actionpack (4.2.7) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.7) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.7) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.7) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.7) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.7) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.7) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.7) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.7) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.7) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.7) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.7) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.7) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.7) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.7) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.7) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.7) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.7) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.7) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:74:in `call'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:43:in `serve'
actionpack (4.2.7) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.7) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.7) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.7) lib/action_dispatch/routing/route_set.rb:817:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
remotipart (1.2.1) lib/remotipart/middleware.rb:27:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.7) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.7) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.7) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.7) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.7) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.7) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.7) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.7) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.7) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.7) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.7) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.7) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.7) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.7) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.7) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.7) lib/rails/engine.rb:518:in `call'
railties (4.2.7) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
Request
Parameters:
None
Toggle session dump
_csrf_token: "DK6t1OuVBdMWZLihVgktZFIQSo7gX3qt5Iq5ZHyA7lM="
session_id: "2391dc6faaafcd52ee1815ccbe115daf"
user_id: 7
Toggle env dump
GATEWAY_INTERFACE: "CGI/1.1"
HTTP_ACCEPT: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
HTTP_ACCEPT_ENCODING: "gzip, deflate, sdch"
HTTP_ACCEPT_LANGUAGE: "en-US,en;q=0.8"
HTTP_CACHE_CONTROL: "max-age=0"
REMOTE_ADDR: "127.0.0.1"
REMOTE_HOST: "127.0.0.1"
SERVER_NAME: "localhost"
SERVER_PROTOCOL: "HTTP/1.1"
Response
Headers:
None
x
>>
I think I would solve this like so:
class Apt < ActiveRecord::Base
enum status: [:draft, :publish, :unpublish, :waiting]
def self.user_specific_statuses(user)
if user.admin? # define a method in your user model which defines this.
Apt.statuses
else
Apt.statuses.reject { |k, v| k == "publish" } # => all except publish
end
end
end
And then you could call it like this:
Apt.user_specific_statuses(admin_user) # => all statuses
Apt.user_specific_statuses(normal_user) # => only draft, unpublish, waiting
i don't know is this an ideal way to do this or not but i solved it with this approach :
i changed my model enum definition to this :
model : enum apt_status: { draft:0, publish:1, unpublish:2, waiting:3 }
and in my controller i did this :
ishaveprivilage = Usermaster.joins(:rolemasters, :rolemasters).where(id: #current_user.id, rolemasters: {name: "Super Admin"})
if !ishaveprivilage.blank?
#apt_statuses = Apt.apt_statuses
else
temp = Apt.apt_statuses.reject { |k,v| v == 1 } #remove Publish option for non super admin
#apt_statuses = temp
end
if you guys have a better approach, please advise.

Resources