How to give ActiveAdmin its own application layout? -- Rails newbie - ruby-on-rails

It's probably a dead simple question. Right now it uses the default application layout file. How can I create one just for AdminAdmin in order keep it separate from the one used on the main site?

Read layouts and rendering guide, there is paragraph about :layout option.
You can specify layout, or set it to false, to render without layout at all.
render :layout => 'special_layout'
render :layout => false

If my memory does not fail me again !! ;-)
this rails cast talk about this. But i'm not sure!
Anyway, if you never watched it. It's a good watch.

Related

Can I Render A Layout Directly From routes.rb, Without A Controller?

I would like to set up a pair of style guides for the admin and public sections of a website.
Each will need its own layout which will contain a mixture of static html and calls to erb partials (so a static page won't cut it). I have no need of a controller(s) to serve these pages and I don't want what is effectively development-only content cluttering up the rest of the code. This got me wondering whether there is a way to render a layout directly.
Disclaimer: I appreciate this is not something I should do often/ever and I know there are a wealth of arguments for why this is a bad idea. I am interested in whether this is possible.
Is there a way for me to render a layout directly from routes.rb without going through a controller?
For some weird reason I wanted to render a blank JS file for a while, and writing a controller felt like too much for this kind of hack. Thanks to #genkilabs 's answer, I used this 3 liner:
get 'analytics/some_file.js', to: -> (env) do
[200, { 'Content-Type' => 'application/javascript' }, ['']]
end
I wanted to do something really stupid once, so if you do too, try this working example.
match :movedpage, :to => proc { |env|
if Rails.env.production?
#remote_path = 'http://productionhost.com'
elsif Rails.env.staging?
#remote_path = 'http://staginghost.com'
else
#remote_path = 'http://localhost:3000'
end
[
200,
{"Content-Type" => "text/html"},
[File.read("public/moved_page.html").gsub('#remote_path', #remote_path)]
]
}, :via => :all
Where moved_page.html was a static page asking people up update their bookmarks and #remote_path just typed in a link like #remote_path. Note that <%= %> won't work because you don't have view helpers in there.
So, theres enough rope to get yourself in trouble ^_^
Actualy the answer is NO, you can't do it without a controller. But see some trivial workaround...
It's not very fair but should work:
Assuming you have FooController with any logic you already have implemented. Now you want to render anypage.html.erb without creating any special controller. Here is how:
Configure a route to your static page:
get '/your/static/page', to: 'foo#anypage'
Implement the view app/views/foo/anypage.html.erb.
The problem is that it is impossible to change a path to your view. The path depends on the controller that you specify in a route (foo in the example). Also note that it will be rendered with a specified for FooController layout.
It should work by convention and you can read about it here.
UPDATE
Also I found very simmilar solution here. Using ApplicationController seems more reasonable for such pages. (Note that you needn't to create an action for it)

How best to setup rails with only one view?

I'm working on a personal RoR project with an interesting sort of problem: the whole app only needs one HTML template.
Basically, the whole app is presented through HTML5 canvas (it's going to be a game of sorts). But I'd still like there to be URLs for accessing specific resources, such as '/player/1'.
So what's the best, DRYest way to do this? I'd really hate to specify the template in every action in the controllers.
render :file => "layout_file", :layout => false
You could define your view in app/views/layout/application.html.erb and leave all the others empty, but that wouldn't avoid the reloading of pages.
You should also have all your methods respond in json format.
Or just an old good:
render :nothing => true
at the end of your methods.

Rails 3.1 partial fallback

I don't quite understand why the new feature of partial rendering doesn't always kick in.
For example with this code:
= render(:partial => "pages/#{foo}/data")
it will never fallback to the partial pages/_data.html.erb if pages/test/_data.html.erb doesn't exist. How can I get this behavior?
Something you can do is :
= render(:partial => "pages/#{foo}/data") rescue render(:partial => "pages/data")
But I would be interested if someone has a better solution, this one is not elegant especially when you have some variables to pass to the partial. At least It can be better if handled by a helper.
My understanding of partials is not that they are small, standalone chunks of HTML to render, but rather bits and pieces of abstraction that you can use to organize your code in a more coherent manner.
As I see it, partials are created to be used in one or more places such that the file does not become cluttered, they're not used to simply be rendered by themselves. I could be mistaken, however.

Rails - any fancy ways to handle 404s?

I have a rails app I built for an old site I converted from another cms (in a non-rails language, hehe). Most of the old pages are mapped to the new pages using routes.rb. But there are still a few 404s.
I am a rails newb so I'm asking if there are any advanced ways to handle 404s. For example, if I was programming in my old language I'd do this:
Get the URL (script_name) that was being accessed and parse it.
Do a lookup in the database for any keywords, ids, etc found in the new URL.
If found, redirect to the page (or if multiple records are found, show them all on a results page and let user choose). With rails I'd probably want to do :status => :moved_permanently I'm guessing?
If not found, show a 404.
Are there any gems/plugins or tutorials you know of that would handle such a thing, if it's even possible. Or can you explain on a high level how that can be done? I don't need a full code sample, just a push in the right direction.
PS. It's a simple rails 3 app that uses a single Content model.
Put this in routes (after every other route that you have, this will capture every url)
match '*url' => 'errors#routing'
And now in errors controller in routing action you can implement any fancy logic that you want, and render a view as always (you might want to add :status => 404 to the render call). Requested url will be available in controller as params[:url].
There is an ugly way of doing this:
render :file => "#{RAILS_ROOT}/public/404.html", :layout => false, :status => 404
Maybe someone can come with a better solution.

Is is possible to include views in a gem that the user can render as a partial?

Say I am making gem "awesome_o" and it will make apps awesome. How could I package up some view partials so that the user can optionally use them in his/her app for eg:
<%= render :partial => '#{some_path_to_awesome_o}/list_of_awesome' %>
Is that possible?
As I understand it, if you create an app/views directory in the base of your gem, Rails adds that to the views load path. So, create your partial at app/views/my_gem/my_partial.html.ext, and then render :partial => 'my_gem/my_partial' should work as expected.
As far as usage goes, though, I'd like you to include a simple helper method, too, since it'd be far easier for me to use and would allow you to change exact implementation later on. Even if it just calls render :partial internally, it'd produce a smoother experience.
Nowadays you could use an engine: http://edgeguides.rubyonrails.org/engines.html
As an alternative you could also make generators to create the views in the rails app, this would allow the users to alter the views to suit their needs.
I guess it depends on exactly what you are doing.

Resources