From 37741f8bd5bc4c3698194c04bbe8aee3d7f6781a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 19 Jul 2017 18:41:05 +0200 Subject: Fix errors from JS-formatted templates using the table_builder We were getting errors like these: Started GET "/referentials/4/networks?_=1500482065840" for 127.0.0.1 at 2017-07-19 18:34:27 +0200 Processing by ReferentialNetworksController#index as JS Parameters: {"_"=>"1500482065840", "referential_id"=>"4"} User Load (3.6ms) SELECT "public"."users".* FROM "public"."users" WHERE "public"."users"."id" = $1 ORDER BY "public"."users"."id" ASC LIMIT 1 [["id", 1]] locale set to :fr Organisation Load (5.6ms) SELECT "public"."organisations".* FROM "public"."organisations" WHERE "public"."organisations"."id" = $1 LIMIT 1 [["id", 1]] Referential Load (1.0ms) SELECT "public"."referentials".* FROM "public"."referentials" WHERE "public"."referentials"."organisation_id" = $1 AND "public"."referentials"."id" = $2 LIMIT 1 [["organisation_id", 1], ["id", 4]] Workbench Load (0.7ms) SELECT "public"."workbenches".* FROM "public"."workbenches" WHERE "public"."workbenches"."id" = $1 LIMIT 1 [["id", 1]] (1.3ms) SELECT DISTINCT COUNT(DISTINCT "public"."networks"."id") FROM "public"."networks" INNER JOIN "public"."line_referentials" ON "public"."networks"."line_referential_id" = "public"."line_referentials"."id" WHERE "public"."line_referentials"."id" = $1 [["id", 1]] CACHE (0.0ms) SELECT DISTINCT COUNT(DISTINCT "public"."networks"."id") FROM "public"."networks" INNER JOIN "public"."line_referentials" ON "public"."networks"."line_referential_id" = "public"."line_referentials"."id" WHERE "public"."line_referentials"."id" = $1 [["id", 1]] Chouette::Network Load (2.3ms) SELECT DISTINCT "public"."networks".* FROM "public"."networks" INNER JOIN "public"."line_referentials" ON "public"."networks"."line_referential_id" = "public"."line_referentials"."id" WHERE "public"."line_referentials"."id" = $1 ORDER BY name asc LIMIT 12 OFFSET 0 [["id", 1]] CACHE (0.0ms) SELECT DISTINCT COUNT(DISTINCT "public"."networks"."id") FROM "public"."networks" INNER JOIN "public"."line_referentials" ON "public"."networks"."line_referential_id" = "public"."line_referentials"."id" WHERE "public"."line_referentials"."id" = $1 [["id", 1]] Chouette::Network Exists (1.2ms) SELECT 1 AS one FROM "public"."networks" WHERE "public"."networks"."id" = $1 LIMIT 1 [["id", 118]] Rendered referential_networks/index.html.slim (48.9ms) Completed 500 Internal Server Error in 184ms (ActiveRecord: 17.4ms) ActionView::Template::Error (undefined method `action_links' for #): 22: - if @networks.any? 23: .row 24: .col-lg-12 25: = table_builder_2 @networks, 26: [ \ 27: TableBuilderHelper::Column.new( \ 28: name: 'ID Codifligne', \ app/helpers/table_builder_helper.rb:161:in `block in build_links' app/helpers/table_builder_helper.rb:158:in `build_links' app/helpers/table_builder_helper.rb:138:in `block (3 levels) in tbody' app/helpers/table_builder_helper.rb:110:in `block (2 levels) in tbody' app/helpers/table_builder_helper.rb:108:in `block in tbody' app/helpers/table_builder_helper.rb:107:in `tbody' app/helpers/table_builder_helper.rb:72:in `table_builder_2' app/views/referential_networks/index.html.slim:25:in `_app_views_referential_networks_index_html_slim___1557411244315414919_70366016986820' app/controllers/referential_networks_controller.rb:27:in `index' Rendered .../lib/ruby/gems/2.3.0/gems/actionpack-4.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (1.8ms) Rendered .../lib/ruby/gems/2.3.0/gems/actionpack-4.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (2.5ms) Rendered .../lib/ruby/gems/2.3.0/gems/actionpack-4.2.8/lib/action_dispatch/middleware/templates/rescues/template_error.text.erb (76.0ms) on the following pages: - http://stif-boiv.dev:3000/referentials/4/time_tables - http://stif-boiv.dev:3000/line_referentials/1/companies - http://stif-boiv.dev:3000/line_referentials/1/networks - http://stif-boiv.dev:3000/referentials/4/companies - http://stif-boiv.dev:3000/referentials/4/networks This was caused by the request of a JS-formatted view that rendered the HTML template, since the collection decoration only happened in the HTML format section. Do the decoration in both the HTML format and JS format for the relevant controllers in order to eliminate this error. Maybe there's a cleaner way to do this than just duplicating the decoration in both `format.X` blocks, but I don't know of it. Wanted to make sure not to decorate when it wasn't necessary, like when rendering other formats (XML, etc.). Refs #4105 --- app/controllers/companies_controller.rb | 23 +++++++++++++++------- app/controllers/networks_controller.rb | 23 +++++++++++++++------- .../referential_companies_controller.rb | 23 +++++++++++++++------- app/controllers/referential_networks_controller.rb | 23 +++++++++++++++------- app/controllers/time_tables_controller.rb | 22 ++++++++++++++------- 5 files changed, 79 insertions(+), 35 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/companies_controller.rb b/app/controllers/companies_controller.rb index 07a732fc9..cf27c39cf 100644 --- a/app/controllers/companies_controller.rb +++ b/app/controllers/companies_controller.rb @@ -16,14 +16,13 @@ class CompaniesController < BreadcrumbController redirect_to params.merge(:page => 1) end - @companies = ModelDecorator.decorate( - @companies, - with: CompanyDecorator, - context: { - referential: line_referential - } - ) + @companies = decorate_companies(@companies) } + + format.json { + @companies = decorate_companies(@companies) + } + build_breadcrumb :index end end @@ -77,4 +76,14 @@ class CompaniesController < BreadcrumbController %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc' end + def decorate_companies(companies) + ModelDecorator.decorate( + companies, + with: CompanyDecorator, + context: { + referential: line_referential + } + ) + end + end diff --git a/app/controllers/networks_controller.rb b/app/controllers/networks_controller.rb index d1f83340e..5dae1ba3f 100644 --- a/app/controllers/networks_controller.rb +++ b/app/controllers/networks_controller.rb @@ -37,14 +37,13 @@ class NetworksController < BreadcrumbController redirect_to params.merge(:page => 1) end - @networks = ModelDecorator.decorate( - @networks, - with: NetworkDecorator, - context: { - line_referential: line_referential - } - ) + @networks = decorate_networks(@networks) } + + format.js { + @networks = decorate_networks(@networks) + } + build_breadcrumb :index end end @@ -87,4 +86,14 @@ class NetworksController < BreadcrumbController %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc' end + def decorate_networks(networks) + ModelDecorator.decorate( + networks, + with: NetworkDecorator, + context: { + line_referential: line_referential + } + ) + end + end diff --git a/app/controllers/referential_companies_controller.rb b/app/controllers/referential_companies_controller.rb index 53dde93bb..482f74ea0 100644 --- a/app/controllers/referential_companies_controller.rb +++ b/app/controllers/referential_companies_controller.rb @@ -14,14 +14,13 @@ class ReferentialCompaniesController < ChouetteController redirect_to params.merge(:page => 1) end - @companies = ModelDecorator.decorate( - @companies, - with: CompanyDecorator, - context: { - referential: referential - } - ) + @companies = decorate_companies(@companies) } + + format.js { + @companies = decorate_companies(@companies) + } + build_breadcrumb :index end end @@ -70,4 +69,14 @@ class ReferentialCompaniesController < ChouetteController %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc' end + def decorate_companies(companies) + ModelDecorator.decorate( + companies, + with: CompanyDecorator, + context: { + referential: referential + } + ) + end + end diff --git a/app/controllers/referential_networks_controller.rb b/app/controllers/referential_networks_controller.rb index e0ce71ce4..ee2db8008 100644 --- a/app/controllers/referential_networks_controller.rb +++ b/app/controllers/referential_networks_controller.rb @@ -30,14 +30,13 @@ class ReferentialNetworksController < ChouetteController redirect_to params.merge(:page => 1) end - @networks = ModelDecorator.decorate( - @networks, - with: ReferentialNetworkDecorator, - context: { - referential: referential - } - ) + @networks = decorate_networks(@networks) } + + format.js { + @networks = decorate_networks(@networks) + } + build_breadcrumb :index end end @@ -81,4 +80,14 @@ class ReferentialNetworksController < ChouetteController %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc' end + def decorate_networks(networks) + ModelDecorator.decorate( + networks, + with: ReferentialNetworkDecorator, + context: { + referential: referential + } + ) + end + end diff --git a/app/controllers/time_tables_controller.rb b/app/controllers/time_tables_controller.rb index b7fb2bf9b..edeb5a32f 100644 --- a/app/controllers/time_tables_controller.rb +++ b/app/controllers/time_tables_controller.rb @@ -86,16 +86,14 @@ class TimeTablesController < ChouetteController redirect_to params.merge(:page => 1) end - @time_tables = ModelDecorator.decorate( - @time_tables, - with: TimeTableDecorator, - context: { - referential: @referential - } - ) + @time_tables = decorate_time_tables(@time_tables) build_breadcrumb :index } + + format.js { + @time_tables = decorate_time_tables(@time_tables) + } end end @@ -195,6 +193,16 @@ class TimeTablesController < ChouetteController Chouette::TimeTable.find(from_id) if from_id end + def decorate_time_tables(time_tables) + ModelDecorator.decorate( + time_tables, + with: TimeTableDecorator, + context: { + referential: @referential + } + ) + end + def time_table_params params.require(:time_table).permit( :objectid, -- cgit v1.2.3