Sinatra Sub-Directory Views
I'm not sure exactly what you're asking, but you can render a view in views/admin
by doing this:
erb :"admin/report"
If you're asking how to automatically look in subdirectories of views
when you call erb :report
, I'm not sure how to do that, and I don't think you'd want to (what happens if two views in different dirs have the same name?).
Sinatra application cannot find views directory
Ok I figured it out. The set :views statement should actually be inside my app class like this:
class MusicCatalog < Sinatra::Base
**set :views, Proc.new { File.join(root, "../views") }**
get "/" do
haml :index
end
end
Also I was joining the root in the wrong way earlier. Fixed that. Now sinatra is correctly loading my templates
Render directory contents with sinatra
Figured it out - you can serve static assets with the sinatra dsl by accepting the file path as a param:
get '/api-docs/:path_1/:path_2' do
path = File.join(settings.public_folder, 'bower_components', 'swagger-ui', 'dist', params[:path_1], params[:path_2])
File.read(File.expand_path(path))
end
get '/api-docs/:path' do
path = File.join(settings.public_folder, 'bower_components', 'swagger-ui', 'dist', params[:path])
File.read(File.expand_path(path))
end
note, you will likely have to set the appropriate mime/content types to responses
Sinatra models access root view folder
You can configure explicit where your views are.
:views - view template directory
A string specifying the directory where view templates are located. By
default, this is assumed to be a directory named “views” within the
application’s root directory (see the :root setting). The best way to
specify an alternative directory name within the root of the
application is to use a deferred value that references the :root
setting:
Example
set :views, Proc.new { File.join(root, "../views") }
Simply add this to your configure methode. How?
According your Example
Shoud it be set :views, Proc.new { File.join(root, "../views") }
from your model. And render it with slim :"song/song"
or slim :"song/edit_song"
source
Not sure but if your routing logic in app.rb you can skip the part with set the view folder.
Build MVC structure on top of Sinatra
Sinatra is already "VC" - you have views separated from your routes (controllers). You can choose to break it into multiple files if you like; for more on that, see this answer (mine):
Using Sinatra for larger projects via multiple files
To add an "M" (model), pick a database framework. Some people like ActiveRecord. Some people like DataMapper. There are many more from which you might choose. I personally love and highly recommend Sequel. My answer linked above also suggests a directory structure and shell for including the models. Once you distribute appropriate logic between your models and controllers, you have your "MVC".
Note that MVC is not about separate files, but separation of concerns. If you set up a Sinatra application as I suggest above, but have your views fetching data from your models, or you have your routes directly generating HTML (not through a "helper"), then you don't really have MVC. Conversely, you can do all of the above in a single file and still have an MVC application. Just put your data-integrity logic in your models (and more importantly, in the database itself), your presentation logic in your views and reusable helpers, and your mapping logic in your controllers.
Related Topics
How to Refresh a Page with Turbolinks
Finding the Difference Between Strings in Ruby
Why Is Rack Response Body an Array Not a String
Rails Console Fails with 'Switch to Inspect Mode' in Windows
Can Multiple Sidekiq Instances Process The Same Queue
How Do Get a Random Datetime Rounded to Beginning of Hour in Rails
Open a File Case-Insensitively in Ruby Under Linux
How to Use Present? in Ruby Projects
Install Bundler Using Rvm, Ruby 1.8.7 with Gcc 4.7 on Linux
Colon in the Front: Yaml Syntax
E: Unable to Locate Package Heroku-Toolbelt
Dynamically Get Object's Attribute
Can You Create/Write/Append a String to a File in a Single Line in Ruby
Failing to Bundle Install Tiny_Tds on MAC Os X 10.8 with Homebrew Freetds