In Rails, how do I override the format from the URL - ruby-on-rails

I have a report that's available in HTML or PDF formats. Sometimes, I want to display an error message instead of the report. The error message should be wrapped in my application's usual layout.
This works fine if the request was for the HTML report, but if the PDF report was requested, I get "There was no default layout for MyController in #<ActionView::PathSet..."
My controller method looks like this:
def report
unless report_available?
render html: '<div class="error">Not available.</div>'.html_safe,
:status => 404, :layout => true
return
end
...
end
I've tried adding :formats => :html or :formats => [:html] to the call to render, but it has no effect. I've also tried setting params[:format] = 'html' before calling render, but that didn't help either.
How do I render a snippet of HTML using the default layout when the request was for report.pdf?
The full error message looks like:
There was no default layout for MyController in #<ActionView::PathSet:0x00007fd3b842c370 #paths=[#<ActionView::OptimizedFileSystemResolver:0x00007fd3d0776c20 #pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", #cache=#<ActionView::Resolver::Cache:0x7fd3d0776bf8 keys=1 queries=0>, #path="/var/www/apps/myapp/releases/1/app/views">]>
And the stack trace looks like:
actionview (5.1.6) lib/action_view/layouts.rb:420:in `_default_layout'
actionview (5.1.6) lib/action_view/layouts.rb:389:in `block in _layout_for_option'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:94:in `resolve_layout'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:74:in `find_layout'
actionview (5.1.6) lib/action_view/renderer/template_renderer.rb:58: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'
/usr/local/rbenv/versions/2.4.3/lib/ruby/2.4.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'
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'
app/controllers/my_controller.rb:809:in `report'
I believe the problem is that it's looking for a layout with .pdf.erb instead of .html.erb, but I don't know how to change that.

What you need to do is redirect to 404 page with this as a flash notice.
If you don't have a 404 page, you'd need one anyway.

You have to set the request format of your action:
def report
request.format = :html
# ..
end
You can also use it in a before_action filter
class ApplicationController < ActionController::Base
before_action :set_default_response_format
protected
def set_default_response_format
request.format = :html
end
end

Related

Undefined method 'id' error using collection_select with form_for in Ruby on Rails

I'm having an issue with collection_select using form_for in rails. This issue is coming from a method that was working until I namespaced my routes to be only for admin. The admin routes are working as they should as far as I can tell, but it is the only connection I can find to why this method stopped working.
Error:
undefined method `id' for #<Array:0x00007faf084c7048>
Here is the trace if that would help:
actionview (6.1.3.2) lib/action_view/helpers/tags/base.rb:41:in `public_send'
actionview (6.1.3.2) lib/action_view/helpers/tags/base.rb:41:in `value'
actionview (6.1.3.2) lib/action_view/helpers/tags/collection_select.rb:18:in `block in render'
actionview (6.1.3.2) lib/action_view/helpers/tags/collection_select.rb:18:in `fetch'
actionview (6.1.3.2) lib/action_view/helpers/tags/collection_select.rb:18:in `render'
actionview (6.1.3.2) lib/action_view/helpers/form_options_helper.rb:203:in `collection_select'
actionview (6.1.3.2) lib/action_view/helpers/form_options_helper.rb:835:in `collection_select'
app/views/admin/users/_edit_my_profile.html.erb:28
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (6.1.3.2) lib/action_view/helpers/form_helper.rb:1004:in `fields_for'
actionview (6.1.3.2) lib/action_view/helpers/form_helper.rb:2195:in `fields_for'
app/views/admin/users/_edit_my_profile.html.erb:16
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:45:in `block in capture'
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
actionview (6.1.3.2) lib/action_view/helpers/capture_helper.rb:45:in `capture'
actionview (6.1.3.2) lib/action_view/helpers/form_helper.rb:450:in `form_for'
app/views/admin/users/_edit_my_profile.html.erb:3
actionview (6.1.3.2) lib/action_view/base.rb:247:in `public_send'
actionview (6.1.3.2) lib/action_view/base.rb:247:in `_run'
actionview (6.1.3.2) lib/action_view/template.rb:154:in `block in render'
activesupport (6.1.3.2) lib/active_support/notifications.rb:205:in `instrument'
actionview (6.1.3.2) lib/action_view/template.rb:345:in `instrument_render_template'
actionview (6.1.3.2) lib/action_view/template.rb:152:in `render'
actionview (6.1.3.2) lib/action_view/renderer/partial_renderer.rb:285:in `block in render_partial_template'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.3.2) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `instrument'
actionview (6.1.3.2) lib/action_view/renderer/partial_renderer.rb:280:in `render_partial_template'
actionview (6.1.3.2) lib/action_view/renderer/partial_renderer.rb:271:in `render'
actionview (6.1.3.2) lib/action_view/renderer/renderer.rb:81:in `render_partial_to_object'
actionview (6.1.3.2) lib/action_view/renderer/renderer.rb:27:in `render_to_object'
actionview (6.1.3.2) lib/action_view/renderer/renderer.rb:22:in `render'
actionview (6.1.3.2) lib/action_view/helpers/rendering_helper.rb:38:in `block in render'
actionview (6.1.3.2) lib/action_view/base.rb:273:in `in_rendering_context'
actionview (6.1.3.2) lib/action_view/helpers/rendering_helper.rb:34:in `render'
app/views/admin/users/profile.html.erb:14
actionview (6.1.3.2) lib/action_view/base.rb:247:in `public_send'
actionview (6.1.3.2) lib/action_view/base.rb:247:in `_run'
actionview (6.1.3.2) lib/action_view/template.rb:154:in `block in render'
activesupport (6.1.3.2) lib/active_support/notifications.rb:205:in `instrument'
actionview (6.1.3.2) lib/action_view/template.rb:345:in `instrument_render_template'
actionview (6.1.3.2) lib/action_view/template.rb:152:in `render'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:61:in `block (2 levels) in render_template'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.3.2) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `instrument'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:56:in `block in render_template'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:71:in `block in render_with_layout'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.3.2) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `instrument'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:70:in `render_with_layout'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:55:in `render_template'
actionview (6.1.3.2) lib/action_view/renderer/template_renderer.rb:11:in `render'
actionview (6.1.3.2) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.1.3.2) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.1.3.2) lib/action_view/rendering.rb:117:in `block in _render_template'
actionview (6.1.3.2) lib/action_view/base.rb:273:in `in_rendering_context'
actionview (6.1.3.2) lib/action_view/rendering.rb:116:in `_render_template'
actionpack (6.1.3.2) lib/action_controller/metal/streaming.rb:218:in `_render_template'
actionview (6.1.3.2) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.1.3.2) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.1.3.2) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.1.3.2) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.1.3.2) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:46:in `block (2 levels) in render'
/home/taylor/.rvm/rubies/ruby-2.6.1/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
activesupport (6.1.3.2) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:46:in `block in render'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:86:in `cleanup_view_runtime'
activerecord (6.1.3.2) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:45:in `render'
actionpack (6.1.3.2) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.1.3.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.1.3.2) lib/abstract_controller/base.rb:228:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.1.3.2) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
actiontext (6.1.3.2) lib/action_text/rendering.rb:20:in `with_renderer'
actiontext (6.1.3.2) lib/action_text/engine.rb:55:in `block (4 levels) in <class:Engine>'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:126:in `instance_exec'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:137:in `run_callbacks'
actionpack (6.1.3.2) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `block in instrument'
activesupport (6.1.3.2) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.1.3.2) lib/active_support/notifications.rb:203:in `instrument'
actionpack (6.1.3.2) lib/action_controller/metal/instrumentation.rb:33:in `process_action'
actionpack (6.1.3.2) lib/action_controller/metal/params_wrapper.rb:249:in `process_action'
activerecord (6.1.3.2) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.1.3.2) lib/abstract_controller/base.rb:165:in `process'
actionview (6.1.3.2) lib/action_view/rendering.rb:39:in `process'
actionpack (6.1.3.2) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (6.1.3.2) lib/action_controller/metal.rb:254:in `dispatch'
actionpack (6.1.3.2) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (6.1.3.2) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.1.3.2) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (6.1.3.2) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.1.3.2) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.1.3.2) lib/action_dispatch/routing/route_set.rb:842: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.1.3.2) lib/action_dispatch/http/permissions_policy.rb:22:in `call'
actionpack (6.1.3.2) 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.1.3.2) lib/action_dispatch/middleware/cookies.rb:689:in `call'
activerecord (6.1.3.2) lib/active_record/migration.rb:601:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.1.3.2) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (6.1.3.2) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/debug_exceptions.rb:29: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:28: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.1.3.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.1.3.2) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.1.3.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.1.3.2) lib/active_support/tagged_logging.rb:99:in `block in tagged'
activesupport (6.1.3.2) lib/active_support/tagged_logging.rb:37:in `tagged'
activesupport (6.1.3.2) lib/active_support/tagged_logging.rb:99:in `tagged'
railties (6.1.3.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/request_id.rb:26:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.1.3.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/static.rb:24:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.1.3.2) lib/action_dispatch/middleware/host_authorization.rb:98:in `call'
webpacker (5.4.0) lib/webpacker/dev_server_proxy.rb:25:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.1.3.2) lib/rails/engine.rb:539:in `call'
puma (5.3.2) lib/puma/configuration.rb:249:in `call'
puma (5.3.2) lib/puma/request.rb:77:in `block in handle_request'
puma (5.3.2) lib/puma/thread_pool.rb:338:in `with_force_shutdown'
puma (5.3.2) lib/puma/request.rb:76:in `handle_request'
puma (5.3.2) lib/puma/server.rb:438:in `process_client'
puma (5.3.2) lib/puma/thread_pool.rb:145:in `block in spawn_thread'
Here is the full method:
<%= user_builder.fields_for :team_attributes, [:admin, #team] do |team_builder| %>
<%= team_builder.hidden_field :profile, :value => "profile" %>
<% if #company.teams.all == [] %>
<br><h3>Create a New Team:</h3>
Team Name: <%= team_builder.text_field :name %>
Team Description: <%= team_builder.text_area :description %><br><br>
<% else %>
<% if #show_form == "new-position" %>
Change Team: <%= team_builder.collection_select :id, #company.teams, :id, :name, {:include_blank => true} %>
Or <%= link_to "Create a new Team", "/admin/profile/#{#user.id}/edit/new-team-and-new-position" %><br><br>
<% elsif #show_form == "new-team" %>
<% else %>
Change Team: <%= team_builder.collection_select :id, #company.teams, :id, :name, {:include_blank => true} %>
Or <%= link_to "Create a new Team", "/admin/profile/#{#user.id}/edit/new-team" %><br><br>
<% end %>
<% end %>
The error is being throw here:
Change Team: <%= team_builder.collection_select :id, #company.teams, :id, :name, {:include_blank => true} %>
I threw in a pry and returned the value of #company and the value of #company.teams:
#<Company:0x00007faf085aa668
id: 1,
name: "My Company",
industry: "Retail",
address: "1546 Jefferson Dr.",
city: "Townesville",
state: "AA",
phone_number: "1232345678",
email: "info#info.com",
user_id: 1,
created_at: Mon, 07 Jun 2021 22:31:22.169619000 UTC +00:00,
updated_at: Mon, 07 Jun 2021 22:31:22.169619000 UTC +00:00>
[#<Team:0x00007faf00375170
id: 2,
name: "Marketing",
description:
"Develops and implements marketing campaigns from previously developed strategies.",
company_id: 1,
user_id: 1,
created_at: Mon, 07 Jun 2021 22:52:15.767441000 UTC +00:00,
updated_at: Mon, 07 Jun 2021 22:52:15.767441000 UTC +00:00>]
This is attempting to create a drop down of a company's team dependent on if the company has teams and depending on what links the user has clicked to reveal parts of the full form. Basically this error is preventing some substantial functionality and progress towards completing this application. If anyone needs more information to help please let me know. Thanks for the help.
Solved it :/ there was no need for me to namespace #teams in fields_for when the original form_for was already namespaced. Don't go to fast people lol

Can't seem to get hotwire-rails `turbo_stream_from` working

I installed the hotwire-rails gem and run rails hotwire:install, which seems to work fine.
When I call the method turbo_stream_from "posts" from a view, I receive the following error, which I don't understand at all.
undefined method `then' for "posts":String
In multiple examples I see a string being passed to the method turbo_stream_from, to identify the streamname. I tried checking the repo but I didn't find much based upon my knowledge of what I saw. Does anybody know what pieces I might be missing to get this working?
turbo-rails (0.5.1) app/channels/turbo/streams/stream_name.rb:21:in `stream_name_from'
turbo-rails (0.5.1) app/channels/turbo/streams/stream_name.rb:19:in `block in stream_name_from'
turbo-rails (0.5.1) app/channels/turbo/streams/stream_name.rb:19:in `map'
turbo-rails (0.5.1) app/channels/turbo/streams/stream_name.rb:19:in `stream_name_from'
turbo-rails (0.5.1) app/channels/turbo/streams/stream_name.rb:13:in `signed_stream_name'
turbo-rails (0.5.1) app/helpers/turbo/streams_helper.rb:20:in `turbo_stream_from'
app/views/forms/index.html.haml:6
actionview (6.0.3.5) lib/action_view/base.rb:274:in `_run'
actionview (6.0.3.5) lib/action_view/template.rb:185:in `block in render'
activesupport (6.0.3.5) lib/active_support/notifications.rb:182:in `instrument'
actionview (6.0.3.5) lib/action_view/template.rb:385:in `instrument_render_template'
actionview (6.0.3.5) lib/action_view/template.rb:183:in `render'
actionview (6.0.3.5) lib/action_view/renderer/template_renderer.rb:58:in `block (2 levels) in render_template'
actionview (6.0.3.5) lib/action_view/renderer/abstract_renderer.rb:88:in `block in instrument'
activesupport (6.0.3.5) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.5) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.5) lib/active_support/notifications.rb:180:in `instrument'
actionview (6.0.3.5) lib/action_view/renderer/abstract_renderer.rb:87:in `instrument'
actionview (6.0.3.5) lib/action_view/renderer/template_renderer.rb:57:in `block in render_template'
actionview (6.0.3.5) lib/action_view/renderer/template_renderer.rb:65:in `render_with_layout'
actionview (6.0.3.5) lib/action_view/renderer/template_renderer.rb:56:in `render_template'
actionview (6.0.3.5) lib/action_view/renderer/template_renderer.rb:13:in `render'
actionview (6.0.3.5) lib/action_view/renderer/renderer.rb:61:in `render_template_to_object'
actionview (6.0.3.5) lib/action_view/renderer/renderer.rb:29:in `render_to_object'
actionview (6.0.3.5) lib/action_view/rendering.rb:117:in `block in _render_template'
actionview (6.0.3.5) lib/action_view/base.rb:304:in `in_rendering_context'
actionview (6.0.3.5) lib/action_view/rendering.rb:116:in `_render_template'
actionpack (6.0.3.5) lib/action_controller/metal/streaming.rb:218:in `_render_template'
actionview (6.0.3.5) lib/action_view/rendering.rb:103:in `render_to_body'
actionpack (6.0.3.5) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (6.0.3.5) lib/action_controller/metal/renderers.rb:142:in `render_to_body'
actionpack (6.0.3.5) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (6.0.3.5) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (6.0.3.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (6.0.3.5) lib/active_support/core_ext/benchmark.rb:14:in `block in ms'
/Users/renevanpelt/.rvm/rubies/ruby-2.5.1/lib/ruby/2.5.0/benchmark.rb:308:in `realtime'
activesupport (6.0.3.5) lib/active_support/core_ext/benchmark.rb:14:in `ms'
actionpack (6.0.3.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (6.0.3.5) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (6.0.3.5) lib/active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
actionpack (6.0.3.5) lib/action_controller/metal/instrumentation.rb:43:in `render'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:46:in `call'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:46:in `render_with_wicked_pdf'
wicked_pdf (2.1.0) lib/wicked_pdf/pdf_helper.rb:30:in `render'
actionpack (6.0.3.5) lib/action_controller/metal/implicit_render.rb:35:in `default_render'
actionpack (6.0.3.5) lib/action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
actionpack (6.0.3.5) lib/action_controller/metal/basic_implicit_render.rb:6:in `tap'
actionpack (6.0.3.5) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.3.5) lib/abstract_controller/base.rb:195:in `process_action'
actionpack (6.0.3.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.3.5) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.0.3.5) lib/active_support/callbacks.rb:112:in `block in run_callbacks'
activesupport (6.0.3.5) lib/active_support/core_ext/time/zones.rb:66:in `use_zone'
app/controllers/dashboard_controller.rb:32:in `set_time_zone'
activesupport (6.0.3.5) lib/active_support/callbacks.rb:121:in `block in run_callbacks'
activesupport (6.0.3.5) lib/active_support/callbacks.rb:139:in `run_callbacks'
actionpack (6.0.3.5) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.3.5) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.3.5) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.3.5) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.3.5) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.3.5) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.3.5) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.3.5) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.3.5) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.3.5) lib/abstract_controller/base.rb:136:in `process'
Update: I've generated a fresh rails 6 app and it works perfectly. The app that gives the error is just migrated from rails 5 to rails 6. Maybe that's where the difference between the two cases is.
Just run into the same issue today. Turns out I am using ruby 2.5 but then was introduced in ruby 2.6.
Upgrading ruby to 2.6.6 fixed the issue.

Render and cache a heterogenous collection in Rails 5

I'm working on an activity stream and have an Event model with subclasses for each type of event, such as StatusChanged etc.
Each type of event has a corresponding partial that formats the event for rendering.
Rails can render heterogenous collections in this manner, and its working out just fine until I try to cache the collection with the cached: true option on the render call.
Here's what I'm working on:
= render partial: events, as: :event, cached: true
where events is the heterogenous collection.
I've also overridden the to_partial_path on event so that partials can be stored in events/_event_type.html.haml vs events/event_type/_event_type.html.haml:
# Render partials from events/_<type> rather than default events/<type>/_<type>
def to_partial_path
self.class.name.underscore
end
Here's the top of the stack trace for the issue I'm running into:
NoMethodError - undefined method `virtual_path' for nil:NilClass:
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:35:in `expanded_cache_key'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:30:in `block in collection_by_cache_keys'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:29:in `collection_by_cache_keys'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer/collection_caching.rb:15:in `cache_collection_render'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:326:in `block in render_collection'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:319:in `render_collection'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:308:in `render'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:21:in `render'
actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:32:in `render'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
haml (4.0.7) lib/haml/helpers.rb:89:in `non_haml'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
app/views/job_applications/_activity.html.haml:7:in `_app_views_job_applications__activity_html_haml__883799091596117587_70254600015120'
actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in `render_partial'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:in `block in render'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:310:in `render'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:21:in `render'
actionview (5.0.0.1) lib/action_view/helpers/rendering_helper.rb:32:in `render'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
haml (4.0.7) lib/haml/helpers.rb:89:in `non_haml'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
app/views/job_applications/_edit.html.haml:29:in `_app_views_job_applications__edit_html_haml__4043825476406280496_70254604867100'
actionview (5.0.0.1) lib/action_view/template.rb:158:in `block in render'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:348:in `instrument'
actionview (5.0.0.1) lib/action_view/template.rb:156:in `render'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:343:in `render_partial'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:311:in `block in render'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.1) lib/active_support/notifications.rb:164:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.0.0.1) lib/action_view/renderer/partial_renderer.rb:310:in `render'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (5.0.0.1) lib/action_view/renderer/renderer.rb:21:in `render'
actionview (5.0.0.1) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.0.0.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (5.0.0.1) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (5.0.0.1) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.0.0.1) lib/action_controller/metal/renderers.rb:144:in `render_to_body'
actionpack (5.0.0.1) lib/abstract_controller/rendering.rb:26:in `render'
Digging into lib/action_view/renderer/partial_renderer/collection_caching.rb:35 it looks like #template is nil.
Its also notable that this issue did not come up while the collection had only one type of Event in it. As soon as the collection contained two types of events...boom.
Has anyone run into this before? It might be as straightforward as "You can't cache a heterogenous collection in this way".

Render partial with locals

I'm using render with partials hundred times on my app but I've a problem with one and I don't understand why.
I'm calling partial recursively.
#index.html.slim
= render partial: 'medias/special_content', locals: {shops: #shops, shop: #shop}
#medias/_special_content.html.slim
= render partial: 'medias/more_specific', locals: {shops: shops, shop: shop}
#medias/_more_specific.html.slim
- if shops.nil?
#Some code
Here is my error:
undefined local variable or method `shops' for #<#
<Class:0x0000000775c360>:0x007f027cff5c28>
app/views/medias/_more_specific.html.slim:130:in
`_app_views_medias__more_specific_html_slim___1518734296928926472_69824330512820'
actionpack (3.2.17) lib/action_view/template.rb:145:in `block in render'
activesupport (3.2.17) lib/active_support/notifications.rb:125:in `instrument'
actionpack (3.2.17) lib/action_view/template.rb:143:in `render'
actionpack (3.2.17) lib/action_view/renderer/partial_renderer.rb:265:in `render_partial'
actionpack (3.2.17) lib/action_view/renderer/partial_renderer.rb:238:in `block in render'
actionpack (3.2.17) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.17) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.17) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.17) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (3.2.17) lib/action_view/renderer/partial_renderer.rb:237:in `render'
actionpack (3.2.17) lib/action_view/renderer/renderer.rb:41:in `render_partial'
actionpack (3.2.17) lib/action_view/renderer/renderer.rb:15:in `render'
actionpack (3.2.17) lib/action_view/helpers/rendering_helper.rb:24:in `render'
app/views/medias/_special_content.html.slim:20:in
...
If it's the same partial where the error is originating from, then it looks like you're rendering the more_specific partial from somewhere else (i.e. not from the special_content file) and not passing the required variables through locals. Because otherwise, I don't think this error is possible.
You could also try the simple render syntax, like so:
render path_to_parent_partial, shops: #shops, shop: #shop # in index
render path_to_child_partial, shops: shops, shop: shop # in parent
But first, search for 'more_specific' in your project and see where you aren't passing the shops variable. You can also check the Rails Server logs output to see which view files are being rendered.
You can direct call #shops in #medias/_more_specific.html.slim because it is instance variable so it would also available here.
if #shops.blank?

Can't use a model method in views

I'm creating a ticket booking app as my sample project using Ruby on Rails 4.1. Three are three models - Events, Tickets and Bookings. Events have many tickets and bookings. Tickets have many bookings and they belong to events. Bookings belongs to events and tickets.
The tickets controller looks like:
class TicketsController < ApplicationController
def index
#event = Event.find(params[:event_id])
#tickets = #event.tickets.all
end
def show
#event = Event.find(params[:event_id])
#ticket = #event.tickets.find(params[:id])
end
def new
#event = Event.find(params[:event_id])
#ticket = Ticket.new
end
def create
#event = Event.find(params[:event_id])
#ticket = #event.tickets.create(ticket_params)
if #ticket.save
redirect_to [#event, #ticket]
else
render 'new'
end
end
def edit
#event = Event.find(params[:event_id])
#ticket= #event.tickets.find(params[:id])
end
def update
#event = Event.find(params[:event_id])
#ticket = #event.tickets.find(params[:id])
if #ticket.update(ticket_params)
redirect_to [#event, #ticket]
else
render 'edit'
end
end
def destroy
#event = Event.find(params[:event_id])
#ticket = #event.tickets.find(params[:id])
#ticket.destroy
redirect_to event_tickets_path
end
private
def ticket_params
params.require(:ticket).permit(:ticket_name, :booking_start_date, :booking_end_date, :ticket_price, :ticket_quantity, :minimum_quantity, :maximum_quantity, :terms_conditions, :more_information)
end
end
The ticket model looks like this
class Ticket < ActiveRecord::Base
belongs_to :event
has_many :bookings
def maximum_tickets_allowed
max = ticket.maximum_quantity.to_i
self.maximum_quantity = (1..max).to_a
end
end
The Tickets show file looks like:
<h2>Tickets</h2>
<p><%= #ticket.ticket_name %></p>
<p><%= #ticket.booking_start_date %></p>
<p><%= #ticket.booking_end_date %></p>
<p><%= #ticket.maximum_tickets_allowed %></p>
maxium_quantity is the maximum number of tickets a person can book and I'm trying here to convert it to an array to be used in collection_select.
Now, if I use <%= #ticket.maximum_tickets_allowed %> I get the undefined methodmaximum_tickets_allowed'error. I tried usingself.maximum_tickets_allowed` but it doesn't work. Where am I going wrong?
Full Stack Trace of the error:
activemodel (4.1.1) lib/active_model/attribute_methods.rb:435:in `method_missing'
activerecord (4.1.1) lib/active_record/attribute_methods.rb:206:in `method_missing'
app/views/tickets/show.html.erb:6:in `_app_views_tickets_show_html_erb__2112154808966627486_70166705061960'
actionview (4.1.1) lib/action_view/template.rb:145:in `block in render'
activesupport (4.1.1) lib/active_support/notifications.rb:161:in `instrument'
actionview (4.1.1) lib/action_view/template.rb:339:in `instrument'
actionview (4.1.1) lib/action_view/template.rb:143:in `render'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionview (4.1.1) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
actionview (4.1.1) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionview (4.1.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.1.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.1.1) lib/action_view/rendering.rb:99:in `_render_template'
actionpack (4.1.1) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.1.1) lib/action_view/rendering.rb:82:in `render_to_body'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.1.1) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
actionpack (4.1.1) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.1.1) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676: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.1.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/flash.rb:254: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.1.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) 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.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144: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'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/mohan/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
You're invoking ticket.maximum_quanitity inside the method, should it not just be maximum_quantity since you're using a ticket instance method?
Some things to think about.
From the comments, it sounds like maybe the #ticket variable is nil? I'm not sure if #event.tickets.find(params[:id]) will throw an ActiveRecord::RecordNotFoundError like Ticket.find will. The way you get data for your controllers is kind of butting heads with the Rails way. There is a one to many relationship between Ticket and Event. There is no need to pass an event_id and id to the TicketsController#show method. Just pass the ticket Id:
class TicketsController < ApplicationController
def show
#ticket = Ticket.find params[:id]
#event = #ticket.event
end
end
Using Ticket.find will raise an ActiveRecord::RecordNotFoundError if the ticket doesn't exist. If I remember correctly, the default controller error handling in Rails should trap that specific exception and render a 404 Not Found page, so after Ticket.find you are assured to have a Ticket object.
Next, if you still need an #event instance variable, use the ActiveRecord relations to get it via #ticket.event.
Now the model itself would need the following code:
class Ticket < ActiveRecord::Base
belongs_to :event
has_many :bookings
def maximum_tickets_allowed
(1..self.maximum_quantity).to_a
end
end
The maximum_tickets_allowed method should just return an array. I'm also wondering if the method call as the end value of the Range is tripping up the Ruby interpreter. You could try instantiating a Range object directly:
def maximum_tickets_allowed
Range.new(1, maximum_quantity).to_a
end
The view remains unchanged:
<h2>Tickets</h2>
<p><%= #ticket.ticket_name %></p>
<p><%= #ticket.booking_start_date %></p>
<p><%= #ticket.booking_end_date %></p>
<p><%= #ticket.maximum_tickets_allowed %></p>
Edit #1: As the comments above mention, make sure you've run all the Active Record migrations in case the maximum_quantity column in the database has been added recently, otherwise that method will not be auto generated by ActiveRecord when the Rails app boots up.
Edit #2: In your show method, you are finding the Event, then all tickets and grabbing only one of the tickets available to the event. That's not as efficient as grabbing the ticket, since you've got the id, and then grabbing the event from the ticket object.
I didn't find any documentation on the find method of an ActiveRecord collection, so I'm not able to validate its behavior.

Resources