Search code examples
model-view-controllerviewsinatramodelscontrollers

Sinatra models access root view folder


I am stuck trying to figure out how to get my models to access the root view folder in sinatra.

File Structure

config.ru
app.rb
models/
  song.rb
views/
  song/
    song.slim
    edit_song.slim
  layout.slim
  login.slim

When I go to view the '/song' route via song.rb, the controller tells it to render the song.slim view. However, when I do that, it ends up looking for app/models/views/song/song.slim instead of app/views/song/song.slim which is the one I want.


Solution

  • 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.