In Rails, how do I override the format from the URL












2















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.










share|improve this question




















  • 1





    Maybe something like this? stackoverflow.com/a/7577829/1577357

    – Zulhilmi Zainudin
    Nov 28 '18 at 16:57











  • @ZulhilmiZainudin, I don't want to handle different formats differently. I want to serve everyone the same HTML error page.

    – cjm
    Nov 28 '18 at 17:05











  • It might be simplest to redirect to the html version of the page if unavavailbe and pdf. You could have the exact same logic in report if you made the not found a unique action. Added as a comment, as it is more of a workaround than a direct answer, which would probably look a lot like what @ZulhilmiZainudin suggested, and you have already expressed reluctance about.

    – Brad Werth
    Nov 28 '18 at 17:23











  • Please include full detail on the error you are receiving.

    – byrdEmmanuel
    Nov 28 '18 at 18:22
















2















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.










share|improve this question




















  • 1





    Maybe something like this? stackoverflow.com/a/7577829/1577357

    – Zulhilmi Zainudin
    Nov 28 '18 at 16:57











  • @ZulhilmiZainudin, I don't want to handle different formats differently. I want to serve everyone the same HTML error page.

    – cjm
    Nov 28 '18 at 17:05











  • It might be simplest to redirect to the html version of the page if unavavailbe and pdf. You could have the exact same logic in report if you made the not found a unique action. Added as a comment, as it is more of a workaround than a direct answer, which would probably look a lot like what @ZulhilmiZainudin suggested, and you have already expressed reluctance about.

    – Brad Werth
    Nov 28 '18 at 17:23











  • Please include full detail on the error you are receiving.

    – byrdEmmanuel
    Nov 28 '18 at 18:22














2












2








2








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.










share|improve this question
















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.







ruby-on-rails ruby-on-rails-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 18:35







cjm

















asked Nov 28 '18 at 16:54









cjmcjm

56.5k8114159




56.5k8114159








  • 1





    Maybe something like this? stackoverflow.com/a/7577829/1577357

    – Zulhilmi Zainudin
    Nov 28 '18 at 16:57











  • @ZulhilmiZainudin, I don't want to handle different formats differently. I want to serve everyone the same HTML error page.

    – cjm
    Nov 28 '18 at 17:05











  • It might be simplest to redirect to the html version of the page if unavavailbe and pdf. You could have the exact same logic in report if you made the not found a unique action. Added as a comment, as it is more of a workaround than a direct answer, which would probably look a lot like what @ZulhilmiZainudin suggested, and you have already expressed reluctance about.

    – Brad Werth
    Nov 28 '18 at 17:23











  • Please include full detail on the error you are receiving.

    – byrdEmmanuel
    Nov 28 '18 at 18:22














  • 1





    Maybe something like this? stackoverflow.com/a/7577829/1577357

    – Zulhilmi Zainudin
    Nov 28 '18 at 16:57











  • @ZulhilmiZainudin, I don't want to handle different formats differently. I want to serve everyone the same HTML error page.

    – cjm
    Nov 28 '18 at 17:05











  • It might be simplest to redirect to the html version of the page if unavavailbe and pdf. You could have the exact same logic in report if you made the not found a unique action. Added as a comment, as it is more of a workaround than a direct answer, which would probably look a lot like what @ZulhilmiZainudin suggested, and you have already expressed reluctance about.

    – Brad Werth
    Nov 28 '18 at 17:23











  • Please include full detail on the error you are receiving.

    – byrdEmmanuel
    Nov 28 '18 at 18:22








1




1





Maybe something like this? stackoverflow.com/a/7577829/1577357

– Zulhilmi Zainudin
Nov 28 '18 at 16:57





Maybe something like this? stackoverflow.com/a/7577829/1577357

– Zulhilmi Zainudin
Nov 28 '18 at 16:57













@ZulhilmiZainudin, I don't want to handle different formats differently. I want to serve everyone the same HTML error page.

– cjm
Nov 28 '18 at 17:05





@ZulhilmiZainudin, I don't want to handle different formats differently. I want to serve everyone the same HTML error page.

– cjm
Nov 28 '18 at 17:05













It might be simplest to redirect to the html version of the page if unavavailbe and pdf. You could have the exact same logic in report if you made the not found a unique action. Added as a comment, as it is more of a workaround than a direct answer, which would probably look a lot like what @ZulhilmiZainudin suggested, and you have already expressed reluctance about.

– Brad Werth
Nov 28 '18 at 17:23





It might be simplest to redirect to the html version of the page if unavavailbe and pdf. You could have the exact same logic in report if you made the not found a unique action. Added as a comment, as it is more of a workaround than a direct answer, which would probably look a lot like what @ZulhilmiZainudin suggested, and you have already expressed reluctance about.

– Brad Werth
Nov 28 '18 at 17:23













Please include full detail on the error you are receiving.

– byrdEmmanuel
Nov 28 '18 at 18:22





Please include full detail on the error you are receiving.

– byrdEmmanuel
Nov 28 '18 at 18:22












2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer
























  • So you're saying it's impossible to override the format from the URL?

    – cjm
    Nov 28 '18 at 17:06



















1














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





share|improve this answer
























  • This has no effect when set in the method. I didn't try a before_action because that would complicate the logic.

    – cjm
    Nov 28 '18 at 17:14













  • None of both? How is your request being formed? This works for my project when the requested url has the form of some.example/resource.html and some.example/resource.json, forcing all requests to answer on .json

    – byrdEmmanuel
    Nov 28 '18 at 17:17













  • My URL looks like example.com/my/123/report or example.com/my/123/report.pdf. The extensionless version works fine; the .pdf one fails.

    – cjm
    Nov 28 '18 at 17:18











  • Perhaps you need to register PDF as a first step, and then you might try this same solution or respond_to do |format| format.pdf { render html: 'your_string'.html_safe } end

    – byrdEmmanuel
    Nov 28 '18 at 17:24











  • As I understand it, respond_to is just for doing different things based on which format was requested. I want to do the same thing no matter what was requested in the URL.

    – cjm
    Nov 28 '18 at 17:30













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53524457%2fin-rails-how-do-i-override-the-format-from-the-url%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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.






share|improve this answer
























  • So you're saying it's impossible to override the format from the URL?

    – cjm
    Nov 28 '18 at 17:06
















2














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.






share|improve this answer
























  • So you're saying it's impossible to override the format from the URL?

    – cjm
    Nov 28 '18 at 17:06














2












2








2







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 17:04









khaled_gomaakhaled_gomaa

2,5351320




2,5351320













  • So you're saying it's impossible to override the format from the URL?

    – cjm
    Nov 28 '18 at 17:06



















  • So you're saying it's impossible to override the format from the URL?

    – cjm
    Nov 28 '18 at 17:06

















So you're saying it's impossible to override the format from the URL?

– cjm
Nov 28 '18 at 17:06





So you're saying it's impossible to override the format from the URL?

– cjm
Nov 28 '18 at 17:06













1














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





share|improve this answer
























  • This has no effect when set in the method. I didn't try a before_action because that would complicate the logic.

    – cjm
    Nov 28 '18 at 17:14













  • None of both? How is your request being formed? This works for my project when the requested url has the form of some.example/resource.html and some.example/resource.json, forcing all requests to answer on .json

    – byrdEmmanuel
    Nov 28 '18 at 17:17













  • My URL looks like example.com/my/123/report or example.com/my/123/report.pdf. The extensionless version works fine; the .pdf one fails.

    – cjm
    Nov 28 '18 at 17:18











  • Perhaps you need to register PDF as a first step, and then you might try this same solution or respond_to do |format| format.pdf { render html: 'your_string'.html_safe } end

    – byrdEmmanuel
    Nov 28 '18 at 17:24











  • As I understand it, respond_to is just for doing different things based on which format was requested. I want to do the same thing no matter what was requested in the URL.

    – cjm
    Nov 28 '18 at 17:30


















1














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





share|improve this answer
























  • This has no effect when set in the method. I didn't try a before_action because that would complicate the logic.

    – cjm
    Nov 28 '18 at 17:14













  • None of both? How is your request being formed? This works for my project when the requested url has the form of some.example/resource.html and some.example/resource.json, forcing all requests to answer on .json

    – byrdEmmanuel
    Nov 28 '18 at 17:17













  • My URL looks like example.com/my/123/report or example.com/my/123/report.pdf. The extensionless version works fine; the .pdf one fails.

    – cjm
    Nov 28 '18 at 17:18











  • Perhaps you need to register PDF as a first step, and then you might try this same solution or respond_to do |format| format.pdf { render html: 'your_string'.html_safe } end

    – byrdEmmanuel
    Nov 28 '18 at 17:24











  • As I understand it, respond_to is just for doing different things based on which format was requested. I want to do the same thing no matter what was requested in the URL.

    – cjm
    Nov 28 '18 at 17:30
















1












1








1







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 17:07









byrdEmmanuelbyrdEmmanuel

866318




866318













  • This has no effect when set in the method. I didn't try a before_action because that would complicate the logic.

    – cjm
    Nov 28 '18 at 17:14













  • None of both? How is your request being formed? This works for my project when the requested url has the form of some.example/resource.html and some.example/resource.json, forcing all requests to answer on .json

    – byrdEmmanuel
    Nov 28 '18 at 17:17













  • My URL looks like example.com/my/123/report or example.com/my/123/report.pdf. The extensionless version works fine; the .pdf one fails.

    – cjm
    Nov 28 '18 at 17:18











  • Perhaps you need to register PDF as a first step, and then you might try this same solution or respond_to do |format| format.pdf { render html: 'your_string'.html_safe } end

    – byrdEmmanuel
    Nov 28 '18 at 17:24











  • As I understand it, respond_to is just for doing different things based on which format was requested. I want to do the same thing no matter what was requested in the URL.

    – cjm
    Nov 28 '18 at 17:30





















  • This has no effect when set in the method. I didn't try a before_action because that would complicate the logic.

    – cjm
    Nov 28 '18 at 17:14













  • None of both? How is your request being formed? This works for my project when the requested url has the form of some.example/resource.html and some.example/resource.json, forcing all requests to answer on .json

    – byrdEmmanuel
    Nov 28 '18 at 17:17













  • My URL looks like example.com/my/123/report or example.com/my/123/report.pdf. The extensionless version works fine; the .pdf one fails.

    – cjm
    Nov 28 '18 at 17:18











  • Perhaps you need to register PDF as a first step, and then you might try this same solution or respond_to do |format| format.pdf { render html: 'your_string'.html_safe } end

    – byrdEmmanuel
    Nov 28 '18 at 17:24











  • As I understand it, respond_to is just for doing different things based on which format was requested. I want to do the same thing no matter what was requested in the URL.

    – cjm
    Nov 28 '18 at 17:30



















This has no effect when set in the method. I didn't try a before_action because that would complicate the logic.

– cjm
Nov 28 '18 at 17:14







This has no effect when set in the method. I didn't try a before_action because that would complicate the logic.

– cjm
Nov 28 '18 at 17:14















None of both? How is your request being formed? This works for my project when the requested url has the form of some.example/resource.html and some.example/resource.json, forcing all requests to answer on .json

– byrdEmmanuel
Nov 28 '18 at 17:17







None of both? How is your request being formed? This works for my project when the requested url has the form of some.example/resource.html and some.example/resource.json, forcing all requests to answer on .json

– byrdEmmanuel
Nov 28 '18 at 17:17















My URL looks like example.com/my/123/report or example.com/my/123/report.pdf. The extensionless version works fine; the .pdf one fails.

– cjm
Nov 28 '18 at 17:18





My URL looks like example.com/my/123/report or example.com/my/123/report.pdf. The extensionless version works fine; the .pdf one fails.

– cjm
Nov 28 '18 at 17:18













Perhaps you need to register PDF as a first step, and then you might try this same solution or respond_to do |format| format.pdf { render html: 'your_string'.html_safe } end

– byrdEmmanuel
Nov 28 '18 at 17:24





Perhaps you need to register PDF as a first step, and then you might try this same solution or respond_to do |format| format.pdf { render html: 'your_string'.html_safe } end

– byrdEmmanuel
Nov 28 '18 at 17:24













As I understand it, respond_to is just for doing different things based on which format was requested. I want to do the same thing no matter what was requested in the URL.

– cjm
Nov 28 '18 at 17:30







As I understand it, respond_to is just for doing different things based on which format was requested. I want to do the same thing no matter what was requested in the URL.

– cjm
Nov 28 '18 at 17:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53524457%2fin-rails-how-do-i-override-the-format-from-the-url%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks