Sinatra not loading layout file when URL has 2 sublevels - ruby

I have this code:
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :admin_users
end
When the view is rendered, I get the HTML in the ERB file but the layout file does not render. If I change the route to just '/users' everything renders fine. What gives? I originally put the users ERB page in a subdirectory 'views/admin' after finding sample code on how to do that and hit this issue. I thought that was the reason but rather it seems its the URL causing the problem. Anyone else hit this or know a work around?
All my other views work fine as well. This is the first view I've tried the URL pattern with. I also tried this but it didn't affect anything.
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :admin_users, :layout => :layout
end
Any help is appreciated. Thanks.

I've got the same problem, check paths to yours assets in the layout. You should add / before.
<link href='/stylesheets/application.css' rel='stylesheet' type='text/css'> is correct.

If you use a subdirectory for the admin_users view, you should really save it as views/admin/users.erb and then render it with
get '/admin/users' do
erb :'admin/users'
end
Maybe this will already address your issue. Let me know, if you need more help.
I guess you can get into trouble when you set the view directory for the admin views different from the location of the layout.erb file. Have you tried putting a dummy layout.erb in the views/admin/ folder?

You did not showed the exact error your are getting. I assume it is "No such file or directory" for the template "admin_users.erb".
An option is missing on your "erb" call and you should tell Sinatra where to find the template. This has just worked for me:
# Users page
get '/admin/users' do
#title = "Admin - Users"
erb :'admin/admin_users', :layout_options => { :views => 'views/admin' }
end
It renders the template on views/admin/admin_users.erb using the layout from views/admin/layout.erb

Related

How to link between two controllers and two views in HTML/erb

I am trying to make simple webboard application. In my question/show view, I have get method to link for action to answer_controller. But it seems not link to the answer view. The error is Sinatra does not know this path. I want to create the path from question/show.erb view to answer/new.erb via submit answer button. What should I do?
question_controller (Controller folder)
get '/questions/:id/show' do
#question = Question.find_by(id: params[:id])
erb :'question/show'
end
question/show (View folder)
<form method="get" controller="answers" action="/answers/question/<%=#question.id%>/new">
<input type=submit value="Answer"></form>
answer_controller (Controller folder)
get 'answers/question/:q_id/new' do
erb :'answer/new'
end
You just need the leading slash in the url of your answer route:
get '/answers/question/:q_id/new' do
# ...

Middleman not rendering markdown and erb

I'm working on a project using Middleman. In one of the pages (videos.html.markdown.erb), I'd like to add partials working with both markdown and Middleman helpers.
<h3><%= video.title %> : RĂ©cit de tournage</h3>
<%= partial "partials/shootandlook1" %>
</div>
It works fine except that Markdown is not converting into HTML... :-(
I named my partial _shootandlook1.html.markdown.erb and my page videos.html.markdown.erb.
I really don't understand what I did wrong... Could someone please help me?
The whole source code is here.
Many, many thanks in advance!
This should work fine if you name your page template file videos.html.erb, and name your content partial _shootandlook1.md.
The Markdown file will be processed first, then inserted into the ERB template appropriately.
I usually find that it's best to avoid having multiple template formats in one file, unless the format explicitly supports blocks (like Haml)

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 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?

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

Resources