Rendering File with Mime Type in Rails

How do I figure out the mime type without writing a file?

Assuming you are uploading file via html form, IO object should already have mime type, you can get it like that:

mime = params[:file].content_type

Render mime typed templates in Rails that are in a subfolder without explicitly providing the whole path in a render call

I was able to restructure the view folders by changing the default view path like suggested here.

show.html.erb rendering as plain text mime time

The Linux admin for my host said they are running a version of Mongrel where this is a known bug.

He put a newer version of mongrel.rb in my /config/initializers and that solved the problem.

Rendering a view with custom mime type in Grails

Do you have the accept header in the request set the custom content-type?

The accept header is one way for client to inform server which content-type would be acceptable for itself.

In config.groovy the below setting has to be set as well to use accept headers

grails.mime.use.accept.header = true

I would also try rendering the response in the conventional way:

render(contentType: "application/vnd.api+json", text: [message: 'some text', foo: 'bar'])

Setting view formats for custom mimetypes in Ruby on Rails?

I am going by your requirement that

I want this format to basically serve the same views as the .html format but with a different layout

I have a different approach that should work in Rails 3 (tested in Rails 3.2.12). Put the following in your controller:

  before_filter do
@bare= (params[:format] == 'modal')

if @bare
params[:format]= 'html'
request.format= :html
end
end

layout :select_layout

# standard controller stuff

# ...
# Towards bottom of your controller code,
private
def select_layout
@bare ? 'bare' : nil
end

Summary:

  • Change the format back to html.
  • Set a variable to indicate that it needs to serve 'bare'
  • Tell controller that value of layout will be given by a function. (See 'Choosing Layouts at Runtime' in http://guides.rubyonrails.org/layouts_and_rendering.html)
  • No changes needed in any of the respond_to blocks.

This will do exactly what you need, i.e., same action as .html but with a different layout.



Related Topics



Leave a reply



Submit