0

I recently began programming and learning Ruby and JavaScript and was attempting to read my html file through my Sinatra server using a config.ru file.

The server runs, its hitting all the routes but I think there may be something wrong with the server code for the index page:

get("/") do
  content_type :html
  File.read( File.expand_path("../views/index.html", __FILE__) )
end
yenshirak
  • 3,021
  • 2
  • 21
  • 27
  • What exactly wrong with your server? index page not load ? – Roman Kiselenko Jan 24 '15 at 09:44
  • Yea that's exactly it. Seems rudimentary but I can't figure it out. I didn't write the code for the server and I'm not sure if there's an error that I'm not seeing – Sol Cameron Jan 24 '15 at 17:36
  • Using console in Google Chrome I can see the html is being loaded at 1280px X 8px – Sol Cameron Jan 24 '15 at 18:42
  • i do not understand your last comment but you can use `send_file` method like this `send_file File.read(....)` – Roman Kiselenko Jan 24 '15 at 18:45
  • So: send_file(File.read( File.expand_path("../views/index.html", __FILE__) )) ? – Sol Cameron Jan 24 '15 at 19:02
  • try this `send_file('views/index.html')` – Roman Kiselenko Jan 24 '15 at 19:04
  • The console in Google Chrome which you can use to interact with the DOM and manipulate using javascript/jQuery. Under its Elements tab I can see all the html loaded into the browser. It shows my html file is being read but I can't seem to interact with it and its loaded the length of the browser (1280p) but oddly at height 8p – Sol Cameron Jan 24 '15 at 19:07

1 Answers1

1

Put index.html in public folder. Sinatra will serve files in public as is. So you need to request it directly e.g. http://localhost/index.html.

If you want to handle empty route i.e. get '/' use snippet below (from here):

get '/' do
  send_file File.join(settings.public_folder, 'index.html')
end

In order to be sure in settings.public_folder please check does it work correctly, does it return correct path.

Cheers!

Community
  • 1
  • 1
sashaegorov
  • 1,821
  • 20
  • 26