More Blog Entries

Creating a Blog application in Rails 3.2

Posted by Gary Benton on Wednesday, June 13, 2012.

Would you like to learn how to create a simple application using Ruby on Rails? Ruby on Rails is becoming very popular because sites can be developed quickly, often in half the time the other technologies require.

In the video below, you'll learn how to create a simple blog application with Ruby on Rails.

John Ash, Senior Developer at K3 Integrations, recently gave a Ruby on Rails presentation in Hong Kong. Here's the part he wasn't able to fit into the presentation:

Here's a question from a viewer on YouTube:

"When I log into a user, and click the logout button, it throws an error and I ca't seem to be able to fix it. Maybe´╗┐ you can give me some insight?

ActiveRecord::RecordNotFound at /users/sign_out
Couldn't find User with id=sign_out

The answer was too long to fit into the reply on the YouTube page, so I'm posting it here:

There's one of two things going on. Either:

1. You have disabled javascript on the page, or:2. You're missing :method=>'delete' on line 5 of app/views/layouts/_navigation.html.erb

Here's what's happening: The logout link is using unobtrusive Javascript (UJS) to send a (simulated) http DELETE to the server to delete the session. It does not send a GET request to /users/sign_out but rather a POST with parameters _method=delete and the associated authenticity_token. You can see this if you watch the request/response using Chrome development tools or Firefox's Firebug. If you view the HTML source on the web page, you will see the logout link has an extra parameter: data-method="delete" That's what tells Rails UJS to send the request as a DELETE rather than a standard "GET". GET requests should not change data or change state. Deleting a session is an action that definitely changes the state.

Now, if you disable JS or don't have the data-method attribute on your logout link, the request to /users/sign_out will go to Rails as a GET request. If you type "rake routes" you will see that there is a DELETE route to /users/sign_out and a GET route to: /users/:id The second route here is what allows us to view details of users by specifying their id in the url, such as: /users/6 for showing the user with the id of 6. If the request to Rails comes in as a GET, then Rails will skip the DELETE route and instead use the GET route, and will attempt to find a user with the ID of "sign_out", which of course fails.

Hope that helps. --John

Here's another question from a viewer:

I'm sorry if the question is too obvious, but when I try to run the rails command I'm getting this : invalid option -T. Can´╗┐ anyone help me out?

Reply from John Ash:

I'm not exactly sure why you're getting an error for -T I tried out my tutorial instructions and the -T worked fine, but I found that the original rails template has been removed. The first command to run has now changed to:rails new gain_blog -m -T Then choose option #4. Ooh. Many of the questions have changed, but it should get you more or less the same app. Let me know if other things break.


More Blog Entries

Post a comment

  • (not displayed)

© 2013 K3 Integrations, LLC | 888.524.6150 |