Using Sessions on Rails 3 and some quick Rails 3 Tips

By default rails uses cookies to store sessions and you should change this.
Using SQLite, creating a sessions table is very easy, just type below one line code to command prompt and you are ready to go..

rake db:sessions:create

You also need to uncomment below line in order use sessions over cookies,

TEST_APP::Application.config.session_store :active_record_store

The above line resides in /app/config/initializers/session_store.rb file
If you change the data type you used to store in sessions table or you just wanna clear the session table, just type below command,
rake db:sessions:clear

And setting the session value is as simple as session[:cart] = nil

NOTE 1: Before rails 3, people used form_remote_tag to create buttons to use in ajax. But after rails3, this tag is deprecated and you should use normal button_to method to create an ajax method with remote=true parameter.

<%= button_to “Ajax me!”, {:action => :action_to_use_ajax_ctrl, :id => btn_id}, :method => ‘post’, :remote => true %>

NOTE 2: if you add ‘error_messages_for‘ to a form to display the errors after client posted form, you’ll see a warning message says: DEPRECATION WARNING: error_messages_for was removed from Rails and is now available as a plugin.
You should use below codes to show errors to the client,

<% if @form_values.errors.any? %>
<% @form_values.errors.full_messages.each do |err_msg| %>
<li><%= err_msg %></li>
<% end %>
<% end %>


Tags: , ,

Comments: 1

Leave a reply »

  • Natasha Mehta

    Thanx a lot….

  • Leave a Reply
    Your gravatar
    Your Name