Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Building Web Apps with Sinatra Updating Data A Route for PUT Requests

Peter Buckwalter
Peter Buckwalter
6,846 Points

Issues with Sinatra wiki app edit stage

Ok so when I update my page it seems to create a new page "%.txt" with the changes rather than actually editing the original page??

I also noticed that if I enter the page title capitalised in the browser it show it capitalised. If I enter it in lowercase I get it in lowercase. ???

require "sinatra"
require "uri"

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

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

get "/" do
  erb :welcome
end

get "/new" do
  erb :new
end

get "/:title" do
  @title = params[:title]
  @content = page_content(@title)
  erb :show
end

get "/:title/edit" do
  @title = params[:title]
  @content = page_content(@title)
  erb :edit
end

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

put "/:title" do
  save_content(params["title"], params["content"])
  redirect URI.escape("/#{params["title"]}")
end

2 Answers

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Your wiki.rb code looks good as far as I can tell. The error is probably in your edit.erb file, which I can't see. There is probably an error in its handling of the title form field.

I also noticed that if I enter the page title capitalised in the browser it show it capitalised. If I enter it in lowercase I get it in lowercase. ???

That's as expected - we're not manipulating the case of the titles at all. On a case-sensitive file system (which some operating systems have, and others don't) it might even create two separate .txt files. If this is an issue, you might want to look into the downcase instance method on Ruby's String class.

Peter Buckwalter
Peter Buckwalter
6,846 Points

Thanks Jay,

I got this sorted after after I set up a head against keyboard action and ran that for a while. It was a couple of typos here and there.

Thanks for getting back to me though.