Showing posts with label rails 3. Show all posts
Showing posts with label rails 3. Show all posts

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.

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 :)

Monday, March 5, 2012

Install mysql gem using bundler in ubuntu 11.10 64 bit

Well just tried doing a mysql gem install using bundler (Gemfile) but got an error like this
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /home/praveen/.rvm/rubies/ruby-1.9.2-p290/bin/ruby extconf.rb
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
and much more
and i forgot to install  libmysqlclient-dev :) how careless! so did
sudo apt-get install libmysqlclient-dev

and redid the bundle install and wohooo it worked!

Thursday, June 2, 2011

How to enable Mongrel instead of Webrick for Rails3 app

To enable mongrel instead of Webrick for Rails3 application add following lines in Gemfile of the app
#Use mongrel as the web server
gem 'mongrel', '>= 1.2.0.pre2'
and do a bundle install and then of you start the server.. mongrel would be booting instead of Webrick

Wednesday, February 9, 2011

How to install ruby 1.9 and rails with RVM

Just follow these steps
ruby -v
mkdir -p ~/.rvm/src/ && cd ~/.rvm/src && rm -rf ./rvm/ && git clone git://github.com/wayneeseguin/rvm.git && cd rvm && ./install
rvm install 1.9.1
rvm list
rvm 1.9.1
rvm 1.9.1 --default
rvm system
gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n
gem install rails --pre
rails topscore
cd topscore
rails server
gem install sqlite3-ruby
rails generate scaffold game name:string
rake db:migrate

and you are done :)
taken from http://railscasts.com/episodes/200-rails-3-beta-and-rvm

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