1

I am following this Sinatra blog post to build my own blog in Ruby Sinatra, the only difference being my templates are in slim and not ERB.

The problem I'm having is in saving edited posts. The posts actually save but it's not redirecting me to the recently edited page and Chrome is giving me a "No data received error", Error code: ERR_EMPTY_RESPONSE.

So my question is how to deal with the No Data Received?

Sinatra Routes

get '/posts/:id/edit' do
    @post = Post.find(params[:id])
    @title = 'Edit Post'

    slim :'posts/edit'
end

put '/posts/:id' do
   @post = Post.find(params[:id])
   if @post.update_attributes(params[:post])
     redirect '/posts/#{@post.id}'
   else
     slim :'posts/edit'
   end
end

Slim Template

h1 Edit Post
  form action="/posts/#{@post.id}" method="post"
    input type="hidden" name="_method" value="put"

    label for="post_title" Title:
    input id="post_title" name="post[title]" type="text" value="#{@post.title}"

    label for="post_body" Body:
    textarea id="post_body" name="post[body]" rows="5" #{@post.body}

    input type="submit" value="Save"

I'm using sqlite3 for the blog database [as said in the blog].

Gumbo
  • 643,351
  • 109
  • 780
  • 844
liloka
  • 1,016
  • 4
  • 14
  • 29

1 Answers1

2

Oh, here's your problem: you have #{...} in the redirect, but it's wrapped by single-quote marks: '. Ruby doesn't interpret interpolations within single-quotes, only within " double-quotes. So if you change that line to redirect "/posts/#{@post.id}" it should work.

acsmith
  • 1,466
  • 11
  • 16
  • 1
    It seems that this depends on which server is being used. Thin happily sends a redirect to `/posts/#{@post.id}` (and which is why I was going to comment saying this answer was wrong) but with Webrick (the default if you have no other server installed) it does indeed raise an exception which results in no data being returned. – matt Jun 22 '14 at 18:04
  • I was using shotgun as the server. And thank you! This worked. I didn't know that about Ruby. – liloka Jun 22 '14 at 19:31