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

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>

Related

Middleman Blog: Custom layout not loading

In Middleman, I am trying to set up a blog site, using custom layout for the blog. My problem is that the main layout is loading, but the blog layout for my articles is not. The article files are being served in with their plain body.
In source/layouts/ I have two files: layout.erb and article_layout.erb.
My intent is to use article_layout.erb for my blog articles.
In config.rb I have the following:
activate :blog do |blog|
blog.sources = "articles/{category}/{year}-{month}-{day}-{title}.html"
blog.layout = "article_layout"
end
I have also tried moving article_layout.erb to source/articles/ as well as prepending the config.rb file like this: blog.layout = "layouts/article_layout"
Another failed approach is to comment out the above option and configure the layout by adding this line instead: page "/articles/*", layout: "article_layout".
So far none of these approach show a difference. Since the default layout is not being rendered I would expect some sort of error message if the path to the layout cannot be found, but nothing shows up.
I managed to replicate your problem with my own Middleman blog setup.
The docs are unclear on this because there is a broken link in the layout section of Blogging.
You need to use the nested layout feature of Middleman and wrap your custom layout in:
<% wrap_layout :layout do %>
<% end %>
So your article_layout.erb would look something like this:
<% wrap_layout :layout do %>
<div class="article-container">
<article>
<h2 class="article-title"><%= current_page.title %></h2>
<%= yield %>
</article>
</div>
<% end %>
And keep your custom layout in the source/layouts folder.
Here are the docs for Nested Layouts for your reference.
I hope this helps.

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

Accessing the name of the page in ERB

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

Rails 3.1 Devise How To Change Flash Message CSS From notice to success?

Rails 3.1 and Devise 1.5 question.
I'm using the following code to display flash messages in my layout:
<% flash.each do |key, message| %>
<%= content_tag(:div, message, :class => "flash #{key}") %>
<% end %>
I'd like to change the css class for some of my confirmation messages from notice to success, but I don't know where to override or change the key because I don't know where it's set.
Can anybody point me in the right direction?
Thanks!
So the way to do this is to edit the devise controllers.
When you install Devise through the normal installation I don't think it install the controllers (at least it didn't for me).
So first you should manually add the controller files and put them into your project in the same location: https://github.com/plataformatec/devise/tree/master/app/controllers/devise
Once you have the files in your project, go to the "sessions_controller.rb" file.
One lines 16 & 25, you should see the code:
set_flash_message :notice, :signed_in
and
set_flash_message :notice, :signed_out
You just need to change ":notice" to ":success"
Hope that works!
I ran into this problem because I'm using the bootstrap-sass gem which uses a whole different class structure for flash messages. I took the route of solving this with sass rather than messing with devise at all (sass inheritance is awesome:).
So, I render the flash messages in app/view/layouts/application.html.haml:
%div.container
- flash.each do |key, value|
= content_tag(:div, value, class: "alert alert-dismisable alert-#{key}")
And, I add a simple rule to app/assets/stylesheets/custom.css.scss to get Devise's flash messages working:
/* Style Devise Flash messages like Bootstrap */
.alert-alert {
#extend .alert-warning;
}
That's it! now flash[:alert] is styled just like bootstrap's flash[:warning].
The css class is being set by the :class argument. To add a class of notice (or success, etc.), just change your call to the following:
<%= content_tag(:div, message, :class => "flash #{key} notice") %>

Rails 3 - link/button to run ajax request without redirecting

What's the best way for a link to run an ajax command (I'm using jquery in Rails 3.1)? So far I am trying to use a link to run code via the controller action "update_me."
views/products/new.html.erb has the following line:
<%= link_to "Update", :action => 'update_me' %>
In my products controller, I have:
def update_me
logger.debug 'ajax code will be here'
end
But this gives a missing template error:
Template is missing
Missing template products/update_me, application/update_me with {:handlers=>[:erb, :builder, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: * "/home/ubuntu/code/preevio/app/views" * "/home/ubuntu/.rvm/gems/ruby-1.9.2-p290#rails31/gems/devise-1.4.3/app/views"
Why do I need to have a update_me.html.erb in my views? I assume that's what they want. Can't I just launch the code in the controller action/method without having to have a view for it? Or am I approaching this the wrong way?
I know this answer is a bit late, but I post it for anyone else who may get here trying to do something similar.
If you change your line of html to
<%= link_to "Update", :action => 'update_me', :remote => true %>
That should have the link to the provided action and do the ajax call for you.
You can look at the provided link to see the docs.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
Since Rails has convention over configuration, Rails expects an appropriate view for the action in the controller.
If you want to render nothing:
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render
render :nothing => true

Resources