Ruby Building Web Apps with Sinatra Finishing Touches Layouts

Paige Jones
Paige Jones
18,273 Points

Anyone willing to look at my Sinatra Wiki App Code? I have some bugs in my code and could use some help

Hi treehouse! Ive just finished the ruby wiki app lessons. However, I did all my code on my local environment using atom. I have some bugs in my code as my stylesheet isn't loading and i get an error when clicking to connected pages.

Would be so appreciative of anyone willing to take a look at my code and give me a hand.

https://github.com/petitlapin86/sinatra

thanks! Paige

Steve Hunter
Steve Hunter
Treehouse Moderator 56,759 Points

Hi Paige,

Happy to try to help - I've never used Sinatra but know Ruby on Rails well.

Does your Github repo have all your folders? I can see three; CSS, pages and views. Is that everything?

Steve.

Paige Jones
Paige Jones
18,273 Points

@stevehunter yes thats all for files! thanks so much - i really appreciate it.

Steve Hunter
Steve Hunter
Treehouse Moderator 56,759 Points

I'm not sure I have an environment set up for Sinatra. Is it Ruby on Rails based? If so, which Ruby version and which Rails version? I can create that in Cloud9.

Paige Jones
Paige Jones
18,273 Points

@stevehunter unfortunately i believe they are different frameworks.

5 Answers

yk7
yk7
21,002 Points

Hello ; disclaimer, I m ruby on rails fun. sinatra is not installed on my computer,

CSS

but I looked at your code; I found that href to reference css file in the layout file,

<link rel="stylesheet" href="/style.css"> 

I think the path should be folder/file.css CSS/style.css the only time that we don't add the folder /. it is when it's public folder >> you see that in challenges practicing with Sinatra ;(see guide (1) on the bottom of the page )

Pages

sorry I could not help with the second issue, of getting an error when clicking on connected pages. But in the File wiki.rb you got some method declared with 1 parameter, like ;

def save_content(title)
  File.read("pages/#{title}.txt", "w") do |file|
    file.print(content)
  end
end

but in the bottom when you call them, you call them with 2 arguments; like this

post "/create" do #setting up post request
  save_content(params["title"], params["content"])
  redirect URI.escape("/#{params["title"]}") 
end

I think you understand the issue, right?! anyway here is the teacher note link:

I hope that helps, and correct me if I m wrong, thx


(1)http://sinatrarb.com/intro.html

(*)http://guides.railsgirls.com/sinatra-app

Paige Jones
Paige Jones
18,273 Points

Hi Thanks SO much for the help. Ive since implemented all the changes you guys suggested, but i'm still not able to get my stylesheet to load and my pages are blank. Any other ideas where my mistake might be?

thank you!

Paige Jones
Paige Jones
18,273 Points

thanks everyone in a combined effort for the help!

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

When visiting the /new path I got:

SyntaxError at /new
/Users/jay/Projects/temp/sinatra/sinatra/views/new.erb:5: syntax error, unexpected ';' ; @_out_buf.concat "\n"

I think the <%- ... -%> here is not valid ERB.

Paige Jones
Paige Jones
18,273 Points

Hi Jay Thanks SO much for the help. Ive since implemented all the changes you guys suggested, but i'm still not able to get my stylesheet to load and my pages are blank. Any other ideas where my mistake might be?

thank you!

Paige Jones
Paige Jones
18,273 Points

Thanks everyone for the suggestions! going to try these out now...

Paige Jones
Paige Jones
18,273 Points

Hi All Thanks SO much for the help. Ive since implemented all the changes you guys suggested, but i'm still not able to get my stylesheet to load and my pages are blank. Any other ideas where my mistake might be?

thank you!

Jay McGavren
Jay McGavren
Treehouse Teacher

I downloaded the latest version of your code.

I confirmed that the wiki pages are blank. So the first step was to figure out why. I added a couple debug calls to p:

def page_content(title)
  p title
  p File.read("pages/#{title}.txt")
rescue Errno::ENOENT
  return nil
end

Then I tried to reload the page, and got this output:

nil
""

So page_content is receiving a title parameter of nil. That led me to look at your call to page_content on line 31:

get "/:title" do #setting up get request
  @title = params[:title] #instance variables
  @content = page_content(params[@title])
  erb :show #render erb template
end

There's the problem! If you're loading a path of /foo, then params[:title] will contain "foo". You set @title = params[:title], as you should. But then you access params[@title], which in this case is the same as accessing params["foo"]. There is no key of "foo" in the params hash, and so you get nil back. page_content(nil) will always return an empty string. And that's why the result is a blank page.

You should try changing line 31 to read like this:

  @content = page_content(@title)

Or this:

  @content = page_content(params[:title])
yk7
yk7
21,002 Points

Hello,

<link rel="stylesheet" href="/CSS/style.css">

For CSS; the href is correctly written, But Sinatra looks for the path in ./public/CSS/style.css So you need to create a folder called public and move the CSS folder to it.

so the real path is different from the path on the layout.erb. Sinatra looks for static files like CSS in the folder ./public, ("." is the root ) but on the layout.erb, you don't to mention the public folder because Sinatra knows where to look for href

reference : http://sinatrarb.com/intro.html > look under section of Static Files

I did test it, and the CSS loads fine, Nice design by the way.

I hope that helps :)