Friday, November 29, 2013

Rails: redirect_to with flash

Hi,

In rails one can provide(not in older versions) flash messages directly in redirect_to method while residing in controller's action to redirect to different path like
redirect_to login_path, :notice => 'Please login'

But one thing needs attention here is, it works only for named routes and not for routes without names like

redirect_to {controller: 'session', action: 'login'}, :notice => 'Please login'

Small but this point should be taken in consideration.

Friday, November 8, 2013

3 column css layout with center fludic and fixed width left right column

How can we make a 3 column layout where left and right most column are with fixed width while the middle column should be fludic (meaning it consumes the whatever is left on screen now)
Concept is pretty simple: Make two two-column layout and insert one into the other :)
Step 01: Make a  two column layout where right column is fixed width and left consumes the left-over screen space.
.container_1 {
   height: auto;
   overflow: hidden;
}
   
.container_1 .right {
    width: 100px;
    float: right;
    background: #DDD;
}

.container_1 .left {
    background: #CCC;
    width: auto;
    overflow: hidden;
}
<div class="container_1">
<div class="right">right content</div>
<div class="left">left content</div>
</div>
Step 02: Make another two column layout where left column is fixed width and right consumes the left-over screen space.
.container_2 {
   height: auto;
   overflow: hidden;
}
   
.container_2 .left_2 {
    background: none repeat scroll 0 0 #BBB;
    float: left;
    width: 180px;
}

.container_2 .right_2 {
    background: none repeat scroll 0 0 #AAA;
    overflow: hidden;
    width: auto;
}
<div class="container_2">
<div class="left_2">left content</div>
<div class="right_2">right content</div>
<div class="clearfix"></div>
</div>

Step 03: Insert container_2 div in container_1's left column div.
Here is the working example
http://jsfiddle.net/praveen_kumar_sinha/fSf8T/


Saturday, May 25, 2013

InvalidAuthenticityToken while file upload in rails 4

Hi Friends,

Am back again and this time really after a long time. Actually occupied with my current company and it is bootstrapping :) wish me luck.

Any how recent problem that i faced was, uploading file (multipart form request) was giving "ActionController::InvalidAuthenticityToken" error in rails4 while a normal form post was working great, which is kind of strange and it was giving error for synchronous and asynchronous request both.

So whats the way to fix it ? 
Add your own authenticity_token params which is missing in rails form (which is strange again) but add a hidden field something like

<%= hidden_field_tag :authenticity_token, form_authenticity_token -%>

and now everything is back to normal :) i hope for you too.

Thanks
Praveen

Friday, February 1, 2013

How to get database size in MySql


SELECT table_schema "DB Name", sum( data_length + index_length ) / 1024 / 1024 "DB Size in MB" 
FROM information_schema.TABLES GROUP BY table_schema ;

Courtesy: http://stackoverflow.com/questions/1733507/how-to-get-size-of-mysql-database

Saturday, September 8, 2012

Rails behaviour of form_for serving from a custom ruby class, (rails2 and rails3 differentiates)

Hi,

It has been so long writing a post and for a change this time it not my laziness but had been really occupied from office front.

Anyhow, here is one strange thing that I found with "form_for" in Rails2 and Rails3.
In Rails2, if we have a class in ruby (not derived from Activerecord::Base) some thing like the one below..

class SomeRubyClass

  attr_accessor :file_name, :file_data, :class_name, :error

  def initialize(file_name, file_data, class_name, error)
    @file_name = file_name
    @file_data = file_data
    @class_name = class_name
    @error = error
  end

  def self.first
    #Some code, which will give an object
  end
end


We can use form_for with it something like this

class SomeController < ApplicationController
  def index
    @some_ruby_class = SomeRubyClass.new("file_name", "file_data", "class_name", "error")
  end

  def create
    @some_ruby_class = SomeRubyClass.first
    @some_ruby_class.class_name = params[:some_ruby_class][:class_name]
    @some_ruby_class.file_name = params[:some_ruby_class][:file_name]
  
    #some more code
    redirect_to :back
  end
end

<% form_for @some_ruby_class, :url => {:action => :create} do |f| %>
    <%= f.text_field :class_name %>
    <%= f.text_field :file_name %>
    <%= f.submit 'Update' %> 
<% end %>

and we can do manipulation as per the need.
But in Rails3 this thing is not supported with form_for
It gives exception like...
undefined method `model_name' for SomeRubyClass:Class' 
and to make it work we have to add these lines 
1. extend ActiveModel::Naming
2. add to_key instance method which must return an array

so added it and result is something like this
class SomeRubyClass
  extend ActiveModel::Naming

  attr_accessor :file_name, :file_data, :class_name, :error

  def initialize(file_name, file_data, class_name, error)
    @file_name = file_name
    @file_data = file_data
    @class_name = class_name
    @error = error
  end

  def self.first
    #Some code, which will give an object
  end

  def to_key
    ["some_key", "some_value"]
  end

end

and it works! Now question arises, Rails3 is imposing me to use ActiveModel (which was initially meant to provide ActiveRecord base type facilities like validation etc.) for simple things like form_for why?? am I missing something here.

Sunday, April 29, 2012

Not able to see skype in the top notification bar in ubuntu

This is something i saw with skype installtion on ubuntu 12.04
Even though skype was running fine, icon in the notification bar was missing (i know it happens sometimes) but it is pretty annoying to bring active skype window in this case. Googled a bit for an answer and found a very sweet and simple answer :)

open a terminal and execute following command
  • gsettings set com.canonical.Unity.Panel systray-whitelist "['all']"
  • unity --reset
and in Ubuntu 12.10 one needs to run
  • unity --reset-icons
And now it is available there after reset

Tuesday, April 24, 2012

AJAX file-upload capability to remote forms in Rails 3. but HOW??

Well this was something strange that I was hitting the other time when was doing the file upload in rails 3.1+ from an AJAX form

I was trying to upload a file in a AJAX form (driven by jQuery) and every time i click submit it used to be normal synchronous request while any submit without file field was going correct as an AJAX request.

Googled it out but not much of help in the beginning but later via some articles came to know people using iframe for file upload on ajax (in rails) how and why this happened I don't know but yes it is there.

I was expected to come out of the situation and complete the work in time, and gem Remotipart came to help...

Will debug it later and currently I don't know much of, how it enables this functionality but it worked flawless even without changing a single line of code in my remote form with file upload.

Just added it to gem file and updated application.js (as is mentioned in the documentation of gem) and it just worked!! 
I know it sounds innocently non-develop-ish, pardon me for that.. will debug more and update soon but for the time being hit the URL here and get yourself out of this situation http://os.alfajango.com/remotipart/

In the mean time anybody want to share any information on this.. most welcome please comment and spread the knowledge :)

Honda Civic & A/C problems.

Hello Friends, Got a post again on to Honda Civic (The good old favorite commuter of mine). This car has been doing great except for so...