I want to use `layout false` but I still want to use the CSS that would typically load when I'm rendering the layout - how do I do this? - ruby-on-rails-3

I have a controller where I set layout to false:
class SplashController < ApplicationController
layout false
def index
end
end
But when I load this page there is no css whatsoever - I assume this has to do with how rails handles layout false - but my current knowledge of rails leaves me lost.
How do I not render a layout, but still load all the other assets (css, js, etc. . .) that would typically load if I were to load a layout? (*Note that the layout file has no specific reference to any of these assets)

By default, if you use the :text option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the :layout => true option.
As you need only the information to be displayed, I suggest to use :text to render.
You can send plain text – with no markup at all – back to the browser by using the :text option to render:
render :text => "OK"
NOTE: Rendering pure text is most useful when you’re responding to AJAX or web service requests that are expecting something other than proper HTML.
UPDATE:
Also if you want that assets should be shown but still layout should be false then you have to render layout to false after making the assets available. This means you make some view, then define your required css and js files there and then call that view from controller and then set layout to false.
Setting the layout to false after view will show the css and js stuff but still keep the layout to false.
But setting the layout to false before showing the view that contains css and js will not include assets at all.
The other alternative of the above will work also:
css : <%= eval("render :partial => 'myurl/blah', :formats=> [:css], :layout => false").dump.html_safe %>
You see that how partial view that contains your assets like :css is getting called while layout is false.

Related

rendering a partial Rails3.x + coffeescript

I have the following requirement. I have a 'school' drop down and as the last options I have add new school, so if the user selects that option I want to load the new_school form as a partial via ajax.
I'm on
gem 'rails', '3.2.9'
gem 'coffee-rails', '~> 3.2.1'
Jquery via gem 'jquery-rails'
Earlier with rails < 3 and prototype I used to do it with
Ajax.Updater (aka Rails link_to_remote :update => 'some_div')
and with rails > 3 + JQuery I'm familiar with *.js.erb, and having something like
$("#school_form").html("<%= escape_javascript(render(:partial => "form"))%>");
But I'm new to coffeescript and I have no idea on how to do this with coffeescript, can someone help me :), (because I believe you shouldn't have to do a server request for this)
So far I have done following to catch the select_tag change event
$ ->
$('#school_name_select').change ->
unless $(this).val()
$('school_name').html([I want to have the _new_school_form partial here])
Use a hidden div.
In general, you don't want to bother trying to mix JS and HTML. The escaping can be complicated, error-prone, and flat out dangerous due to the possibility of cross-site scripting attacks.
Simply render your form partial in a div that's not displayed by default. In ERB:
<div id="school_name_form" style="display: none;">
<%= render 'form' %>
</div>
In your CoffeeScript:
$ ->
$('#school_name_select').change ->
if $(this).val()
$('#school_name_form').slideUp()
else
$('#school_name_form').slideDown()
I recommend using a small, tasteful transition like slide or fade. It gives your app a more polished feel.
No AJAX is required. This pattern is so common that I have an application-wide style defined as follows.
.not-displayed {
display: none;
}
Then using HAML (if you're into that), the HTML template becomes simply:
#school_name_form.not-displayed
= render 'form'
You can try to render the form partial inside hidden div (not too correct from semantic point of view), or put the form html as data attribute of any relevant element, something like
f.select school_name, ... , data: {form: escape_javascript(render(:partial => "form"))}
And the Coffeescript
$ ->
$('#school_name_select').change ->
unless $(this).val()
$('school_name').html($('#school_name_select').data('form'))

Override html in active_admin gem

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

Rails 3: how to render text file in-line?

All.
A Rails n00b here...
I'm writing an application that reports the status of a transaction.
Some of the content in the rendered HTML comes from instance variables
initialized in the controller, while other content comes from text files
(e.g., log files) that I want to render in the HTML using <pre> tags.
What is the "Rails Way" to do this?
Thank you for your time...
<pre>
<%= render :file => '/tmp/test.log' %>
</pre>
Here you go: http://edgeguides.rubyonrails.org/layouts_and_rendering.html
In some cases (when the file is not small and loading it is connected with a delay), I prefer to load the page content and then to use jQuery ajax request to load the file content.
For example, let's say I have a model with file path attribute. In the view layout I am doing something like this:
<pre data-source=" <%= (#file.path) %>"></pre>
Then in the corresponding js file I am loading the context like this:
$(document).ready ->
$.ajax(
url: $("pre").data("source")
context: document.body
).done (response) ->
$("pre").html response
return
return
Of course you can check the jQuery ajax documentation for more options. For example, you can render the pre tag with loading like this:
<pre data-source=" <%= (#file.path) %>"><div class="loading"></pre>
or use other jQuery animations as well.

How do you have a default Gravatar that is external and that actually resizes properly?

To implement Gravatar in my Rails3 application, I'm using the gravatar_image_tag gem in a helper, but I'm having issues when mixing 2 config options:
If the user doesn't have a gravatar attached to his email a default image is rendered; but I want it to reference an external file (e.g., http://www.iconfinder.com/ajax/download/png/?id=43350&s=128 instead of :identicon or others)
I also want the image to be resized on the fly to, let's say 50px.
Independently, both options work as expected, but when I put them together:
def gravatar_for(user, options = { :default => 'http://www.iconfinder.com/ajax/download/png/?id=43350&s=128', :size => 50 })
gravatar_image_tag(user.email.downcase, :alt => user.full_name,
:class => 'gravatar',
:gravatar => options)
end
the size option is not applied, and the gravatar gets rendered in it's full size (128px in this case).
What am I doing wrong, or how can I achieve this combination?
Gravatar will not resize your default image for you. I assume that it just 302s to the ulr gave as a default if it does not find an gravatar for the email you gave it. It looks like the 's' parameter in the iconfinder url is for the size you are trying to grab but that icon does not have a size of 50px available only 128, 256, and 512
Example:
http://www.iconfinder.com/ajax/download/png/?id=43350&s=256
If you wanted a 50px and 80px versions of the icon I would save it to your applications public/image directory as default_gravatar_50.png and default_gravatar_80.png respectively and change your method like so.
end
def gravatar_for(user, options = {})
options = { :size => 50 }.merge(options)
options[:default] = image_tag("default_gravatar_#{options[:size]}.png
gravatar_image_tag(user.email.downcase,
:alt => user.full_name,
:class => 'gravatar',
:gravatar => options)
end
Or if you find an icon on icon finder that is the size(s) you like change the setting of the default option like so.
options[:default] = "http://www.iconfinder.com/ajax/download/png/?id=43350&s=#{options[:size]}"
Iconfinder here. You don't want to link to the download script. Instead just grab the URL to the image it self so you wan't get a lot of header information.

Can I render a text string as a partial in Rails 3?

I am storing customer-specific partials on S3. When I render the value of the S3 object, it renders as text. How can I render it so that it appears within my main layout?
Looks like I just need to:
render :text => myTextFromS3, :layout => true
And it works!
Update: Since 2013 rails changed
There is 3 different ways:
render html: '<strong>HTML String</strong>' # render with `text/html` MIME type
render plain: 'plain text' # render with `text/plain` MIME type
render body: 'raw body' # render raw content, does not set content type, inherits
# default content type, which currently is `text/html`
Source https://github.com/rails/rails/issues/12374