Accessing the name of the page in ERB - ruby

I am building a website with Sinatra and ERB templates. Within the ERB I would like to access the name of the page being loaded and change a small part of the layout accordingly.
For example, my routes are like:
get '/' do
erb :index
end
I need something like:
<% unless page_is_index %>
<!-- HTML goes here -->
<% end %>

There's no direct relation in Sinatra between routing and controllers, so there isn't current_controller and current_action helpers like in Rails. What you can do is checking request.path.

You can specify a different layout which makes the changes you need. If nothing else, this layout could just include the main layout with more options.
get '/' do
erb :index, :layout => 'index_layout'
end

Related

Rails aways renders the application.html.erb instead of wanted views

My Ruby on Rails application always renders the layouts/application.html.erb view instead the views I want it to. Has anyone an Idea why that might be so?
My routes file looks like this:
Rails.application.routes.draw do
root 'startup#index'
resources :users
end
And my application_cotroller.rb is pretty much empty:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
By default, a controller action in Rails renders the view template for your action, wrapped up into a layout (which is application/layout)
ActionView::TemplateHandlers manages the lookup for the extension (.html.erb, .html.haml, .json.erb, etc ...)
so, in an action called index, you will get this implicit call unless you call render yourself :
def edit
# your code
render action :'edit', layout: 'application/layout' # implicitly called
end
Rails will then start processing your layout and put the content of your edit template in place of any yield within your layout. Thus, a typical layout will look like this :
<!doctype html>
<head>
</head>
<body>
<!-- layout content before view -->
<%= yield %>
<!-- layout content after view -->
</body>

Is there a good rails gem to output the view equivalent of rake routes?

What I'm looking for is a gem that could output a complete webpage version of each view a rails app has.
I realize I might not have correctly worded this. What I mean is I would like to see each route that has html output, but what the view actually looks like, not just a line saying what the view is.
In development (rails 4.x) you can simply go to any non-existent page and it will return all of the routes.
Example Screenshot: https://dl.dropboxusercontent.com/u/23115266/Screen%20Shot%202014-02-27%20at%209.21.03%20AM.png
The sextant gem allows you to see all your application's views in development by navigating to the /rails/routes URL.
https://github.com/schneems/sextant
Please correct me if I'm wrong, but it looks like you want a listing of view templates. Not sure there is a gem that can do this, but you could easily do it yourself with the following:
module ShowViewsHelper
def all_views_from_path(path)
helpers = Array(path).flat_map do |_path|
extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_.erb$/
names = Dir["#{_path}/**/*.erb"].map { |file| file.sub(extract, '\1') }
names.sort!
end
helpers.uniq!
helpers
end
end
create a template somewhere with the following:
<ul>
<% all_views_from_path("app/views").each do |v| %>
<li><%= v %></li>
<% end %>
</ul>

Using content_for and yield_content in Ramaze

Sinatra has sinatra/contrib to asist with this, what's the equivalent in Ramaze? I'm using Erubis by the way. Also, a quick Google search shows up really old pages that insist setting variables in the controllers and using them in the views.
Edit 1:
Taken from the gem documentation (http://www.sinatrarb.com/contrib/content_for.html)
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb
<% content_for :some_key do %>
<chunk of="html">...</chunk>
<% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb
<%= yield_content :some_key %>
I don't think Ramaze can do this natively. But you could quite easily do this manually, write a helper to do this, or even fill-in a Hash instance.
You might also want to look at partials if you need to render small chunks of HTML in loops.
You could also combine render_partial, store results in a hash, and yield it's content in the layout.
If the use case is something like rendering a sidebar, you probably want to write a helper so you take the logic out of your views.
A trivial example is here : https://github.com/Ramaze/ramaze/wiki/Adding-a-dynamic-sidebar-in-a-layout

Using ERB for Nesta layout and page templates

I'm trying to get Nesta to use .erb templates for layout and page templates, but confused as to how to go about it.
With partials, it's easy:
= haml :header, :layout => false
becomes:
= erb :header, :layout => false
However, I can't figure out how to have all my pages point to a layout.erb file instead of a layout.haml file.
According to this, .erb templates seem fully supported, though they only describe how to apply them for individual routes. While confusingly in this post it says that "features include support for Erb templates (in the views folder only, not for pages)"?
Does anyone have a better understanding of this?

How to create 1 view for 2 layouts in erb?

I make a project on serve and use erb syntax.
I want to compile two files: index.html (compiled with template content) and load.html (only content). I create two layouts (_clear.html.erb (with the only yield) and _default.html.erb) and create a partial (_content_for_page.html.erb).
There are problems with compilation of index.html.erb.
According to Rails manual (3.4.3 Partial Layouts) I have to write index.html.erb:
<%= render :partial => "_content_for_page.html.erb", :layout => "_default.html.erb" %>
But it doesn't work. Only partial is compiled.
What is wrong?
First I just want to point out Serve does not utilize Rails unless the rails gem is included in the Gemfile of your project, but that will do something unexpected considering Serve organizes your project differently than Rails.
Now I don't know if this answers your question, but it may help to know partials should be rendered without the beginning underscore and are usually passed as a symbol.
<%= render :partial => :content_for_page, :layout => :default %>

Resources