1

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.

SirCharlesWatson
  • 400
  • 1
  • 12

1 Answers1

1

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.

Community
  • 1
  • 1
Sir l33tname
  • 4,026
  • 6
  • 38
  • 49
  • 1
    I've tried that but it doesnt work. I'm trying to access the views in views/songs/*.slim from the folder models/song.rb so in my root directory i have app/models and app/views – SirCharlesWatson May 28 '13 at 07:21
  • 1
    It would be helpful if you could provide a better example of your folder and file structure. – Pablo Jomer May 28 '13 at 08:22
  • ok, after a mostly good night's sleep and a refreshed mind, i tried what Sir Script added to his answer. I think before, I didnt know that i was supposed to set the view from within the model but that makes sense now. I was placing it in the main application file. – SirCharlesWatson May 28 '13 at 14:22