Search code examples
rubysinatraslim-langsqlite3-ruby

No data received Sinatra when editing a blog post


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].


Solution

  • 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.