Newly Installed Layout Redirects Controller Actions to Log Out

Time for another lesson :D

I’ve been banging my head for days because of this particular bug of one of the project I am working on. I installed a new layout for my project to replace the Twitter Bootstrap-provided layout, and upon testing various controller actions, I found them to redirect me to the sign-in page instead of doing the job.

Fortunately, today my co-worker found the culprit: I forgot to include the CSRF (Cross-Site Request Forgery) meta-tags inside the layout’s <head> tag. Hence I insert <%= csrf_meta_tags %> code inside the <head> tag of my layout, and voila! The controllers now stop acting funny.

<head>
	<meta charset="utf-8">
	<title><%= content_for?(:title) ? yield(:title) : "idReads-Web" %></title>
	<link rel="stylesheet" type="text/css" href="/css/blaze/main.css">
	<link href='http://fonts.googleapis.com/css?family=PT+Sans+Narrow:400,700' rel='stylesheet'>
	<%= csrf_meta_tags %>
	<!-- <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel='stylesheet'> -->
	<!--[if lt IE 9]>
		<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
</head>

If you experienced the same behaviour as I, perhaps you should check whether you have included the <%= csrf_meta_tags %> snippet or not.

Passing Variables to Rails Partials

Today’s lesson (for me, a Rails noob :D) is about how to pass variables to Rails partials.

I kept getting error when I tried to pass a variable to a partial with this method:

<%= render :partial => 'admin/publication_transactions/index', :publication_transactions => @publication_transactions %>

It turned out that when I use the :partial key, I need to wrap the variable to be passed inside a hash named :locals. Meanwhile, if I want to pass the variable without :locals hash, I must pass the path as a string instead of using the :partial key. So the correct codes are either this:

<%= render 'admin/publication_transactions/index', :publication_transactions => @publication_transactions %>

or this:

<%= render :partial => 'admin/publication_transactions/index', :locals => { :publication_transactions => @publication_transactions } %>

Thanks to xinuc for pointing out my mistake :)

Language Switch

Starting from 2012, I made a change to this blog. The first one is I began to write in English in this blog. I hope I can increase my writing skill by expressing my thoughts in English. As a consequence, I will also convert all categories’ name into English.

Second, I began to try to separate specific topics into its own blog. I think it is a bad idea to have a blog mixed of everything, that will only make the reader harder to follow my blog. Hence, as a first step I decided to move all backpacking-related topics to IndoHiking, in Indonesian language. Meanwhile, I am thinking to store all IT-related stuff inside this blog.

I hope this move ease the readers to follow my updates. Thanks for visiting!