Wow, what a great site! I hope this question meets the requirements :-)
Generally, this question is about how to set response headers in Rails when using the render method. Specifically, I have a markdown version of a document, which I would like the browser to save as a file by default, rather than display. I have found that you can set headers with the head method, like this:
respond_to do |format|
format.html {...
format.text { head(:content_disposition => "attachment") }
end
But the options for render don't work like this and I can't find anything to access the headers beforehand from the controller. Could anybody offer advice?
Thanks for taking the time to read my question.
yes use #headers method
respond_to do |format|
format.html {...
format.text do
headers[:content_disposition] = "attachment; filename=\"filename.ext\""
render...
end
end
I wasn't sure what the answer was, but this quick search of other articles came up with this: Rails; save a rendered views html content to file
Does that do the trick?
Related
I'm updating a site from rails 4.2 to 5.1
In the previous setup I have page caching on a generated stylesheet (per tenant), all working perfectly.
After upgrading to 5.1 this is no longer working
Using latest version of actionpack-page_caching
Controller for the Stylesheet that is cached looks like this:
class StylesheetsController < ApplicationController
caches_page :show, gzip: true
def show
#stylesheet = Stylesheet.find(params[:id])
respond_to do |format|
format.html
format.css { render text: #stylesheet.contents, content_type: "text/css" }
end
end
end
I'm getting the following error in the logs:
ActionView::MissingTemplate - Missing template stylesheets/show, application/show with {:locale=>[:en], :formats=>[:css], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}. Searched in:
There is no physical template for this as I'm rendering it directly from the stylesheet model. Have confirmed the model is returning data.
Caching is enabled in development.
In the layout page the reference to the dynamic stylesheet is:
<link href="<%= dynamic_stylesheet %>.css" rel="stylesheet" type="text/css" />
and the helper method (in application_helper) is:
def dynamic_stylesheet
stylesheet_path(current_account.stylesheet) unless current_account&.stylesheet&.id.nil?
end
I'm not sure what's getting skipped/missed here, any pointers?
Ok for anyone else who runs into this - the issue is a small change in Rails 5 with render text, in the controller example above it should now read:
format.css { render plain: #stylesheet.contents, content_type: "text/css" }
Found here What to use instead of `render :text` (and `render nothing: true`) in rails 5.1 and later?
I'm having some issue figuring out how to handle success on my entries form using rails 3 ujs ajax.
If there are errors, I have a create.js.erb that will alert(j(#entry.errors.full_messages), and this works. But if there are no errors, the form doesn't redirect (because I'm rendering in a dialog) and I'd like the js to alert("success") and close the dialog. (using fancybox 2).
Can you give me some pointers working with rails 3 ujs and ajax?
An approach is to identify and handle the error at controller level instead of view.
def create
#entry = something
if #entry.save
#notice = "Success message!"
respond_to :js # render default create.js.erb
else
respond_to :js { render 'create_error.js.erb' }
end
end
// create.js.erb
$("#dialog").close();
alert("<%= #notice %>">;
// create_error.js.erb
alert(j(#entry.errors.full_messages);
I have this in my Rails controller:
def download_clip
send_file "public/output.mp4", :type=>"video/mp4", :filename => "output.mp4", :disposition => 'attachment'
end
and in my HTML code I have this:
Now could somebody tell me why Firefox's download window will NOT pou up, but chrome downloads the file fine? Instead firefox opens a new window and starts playing the file. I WANT THE DOWNLOAD BOX to POPUP. I have spend too much time on it
You are using a relative url, which may not map correctly depending on the page it is used.
Try changing your link to:
<%= link_to "some text", :controller => :your_controller_name, :action => :download_clip %>
If this doesn't help, check if the Content-Diposition response header is being set as 'attachment'. If it is, then the problem is likely with your own Firefox environment and not with the server. Resetting Firefox to defaults should fix that...
Add
headers['Content-Disposition'] = "attachment;"
in your download_clip action..
I wanna override html code when working with active_admin gem in Rails; because the nav-bar and many elements in these gem's views are different with my views (other pages). I hope that has a way to change html code without changing css manually! Thanks
It is not very easy , activeadmin use DSL for building html (called "Arbre")
You have to monkey patch every page class, also , it may prevent customizing of css too.
For example to move sidebar to left, create initializer with next patch.
class ActiveAdmin::Views::Pages::Base < Arbre::HTML::Document
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_sidebar unless skip_sidebar?
build_main_content_wrapper
end
end
end
default method was
def build_page_content
build_flash_messages
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
The full list of classes used for rendering can be found here , so some of them you need to patch.
https://github.com/gregbell/active_admin/tree/master/lib/active_admin/views
Be ready for a big piece of work.
UPD. Gem for changing activeadmin sidebar position
https://github.com/Fivell/active_admin_sidebar
I have searched around and have not been able to find a solution for this type of mechanic. I want to load all pages normally in Rails, but whenever I do an ajax request I just want to return the page without the layout. So anytime I make an ajax requst I can append a ?page=true or something along those lines and have Rails just return the page without the layout.
Is this possible? Is there a better way to do it that I am missing?
Thanks for any help!
Final Solution Working Code:
In the controller all you need to do is append a little logic to the format.html in the respond_to block.
In the show method for example
def show
# code beforehand
respond_to do |format|
format.html { render :layout => !request.xhr? }
# other formats
end
end
And that's it! Prevent layouts during AJAX requests!
Note: Thanks to the smathy's comment on his answer this was simplified further. I originally had format.html { render :layout => nil if request.xhr? } This solution works just as well, but smathy's modification keeps it even simpler.
You don't need to add that parameter, request.xhr? will return true in your controller when it's an Ajax request. Just use that to decide whether to render the layout or not.