Dynamically added fields using ajax do not get posted on form submission (FORM inside TABLE)
This is a problem which is actually not a problem if we are following right semantics of HTML.
Problem: I faced this problem where in after a page is loaded I have to add certain input html elements in the form (which I had inside a table) using ajax. Only the elements that were loaded when the page rendered were getting posted, but not the ones added using ajax after the page was loaded. This can be frustrating to figure out why the elements which were loaded by default are getting posted but the elements which rendered after wards using AJAX are not.
My Theory: When a DOM is loaded, the input elements, which are rendered inside TABLE cells get hooked to the FORM automatically because they are rendered when the DOM got loaded. And when the form is posted, its not the table, but the input elements which are hooked to the form get posted. Now, if you are rendering some elements through ajax, then those elements get hooked up only to the TABLE (in case the FORM is inside the TABLE) and not to the form.
Solution: Shift the TABLE inside the FORM. So, that when you add more elements dynamically, it gets hooked to the form as well.
FORM
TABLE — WORKS
TABLE
FORM — NOT GOOD
Rails Fixtures load order
This was a head breaking experience for me recently.
We all know that the fixtures files in rails load by default in alphabetical order. And the records inside every single file again load in alphabetical order (not from top to bottom as you define in the fixture file). Considering that having database level constraints (foreign keys, primary keys etc) is advisable idea in spite of having the rails model level relations (belongs_to, has_many etc), loading fixtures in alphabetical order is the most stupid thing I can ever think of. Imagine you have Account and Employee models and employee has_many accounts. So you have an employee_id sitting as a foreign key in the accounts table. Now you want to load your fixtures (rake db:fixtures:load), for test purpose say, which was my requirement. Since ‘A’ comes before ‘E’ in alphabetical order, rails tries to load the addresses first and then the employees fixture file. Obviously this fails because of foreign key violations. Database raising errors is perfectly making sense here. So I looked around for something which may solve this problem and found that there are solutions available given by some people. But what surprised me is that there is nothing that rails core provided for this.
Anyways, so for people who are into this kinda problem, here is the best one called ActiveFixture that I came across and wish that it would get included in the rails core someday.
Again there is something that may surprise you again. Its not that rails has been stupid all these days to leave us in such a situation. It might so happen that you may not do anything for ordering of the fixtures and still would not get any error although you have foreign key relationships in the database. The reason actually lies in the active record’s database (postgres, mysql sql-lite etc) adapters class. Here is how it goes. While loading the fixtures (I am giving postgres example, as I am using postgres for this project and found it there), rails calls the following method in ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:
def supports_disable_referential_integrity?() #:nodoc:
version = query("SHOW server_version")[0][0].split('.')
(version[0].to_i >= 8 && version[1].to_i >= 1) ? true : false
rescue
return false
end
So this method says, that if you pgsql version is x.y.z, then if x >= 8 and y >= 1, then it will be able to defer the referential integrity for you and you could load the data from fixtures into your database easily without taking care of the ordering and without facing any data integrity errors
Unfortunately I, in my team, was using 9.y.z version of pgsql and all my collegues were on 8.1.4. So they were not getting any errors and I was frustated to know WHY.
Well the secret lies in the ConnectionAdapters for rails
I don’t know if we should take it as an advantage, that rails is trying to do or not, but until we have something more intelligent to infer the order of loading the fixtures files from the relationships in the models, rather than defining the load order manually as mentioned here, I am happy that its saving me some time.
Well after a long time, I finally had to shift back to ROR development on Windows. I am running on Windows Vista, Rails 3, ruby 1.9.2. Things are not all that bad on windows, or at least, not as much as I thought they might be. But, we definitely keep on facing problems here and there. There is one such which you also might face if you try development on Rails 3 with ruby 1.9.2. There seems to be a bug with the gems that are installed with the one click windows ruby installer. I am not sure why, but you may lead to see the following error message once you start using rake:
`bin_path’: can’t find executable rake for rake-0.8.7 (Gem::Exception)
Now the solution to get away from this bug as of this writing is that you will have to remove the rake.gemspec file from the “your_path_to\Ruby192\lib\ruby\gems\1.9.1\specifications” folder.
And you are good to go now. I do not have really too much time to dig into the matter as to why this might be happening, but it worked for me.
Configuring delayed_job 2.x
I was configuring the delayed job and I got problems like :
`method_missing_without_paginate’: undefined method `destroy_failed_jobs=’
This is because I have 2.0.3 version of delayed job… The configuration parameters have all been moved to Delayed::Worker now and thus the following works for version 2.x:
Delayed::Worker.destroy_failed_jobs = false
Delayed::Worker.sleep_delay = 60
Delayed::Worker.max_attempts = 3
Delayed::Worker.max_run_time = 5.minutes
or the following:
Delayed::Worker.destroy_failed_jobs = false
silence_warnings do
Delayed::Worker.const_set("MAX_ATTEMPTS", 3)
Delayed::Worker.const_set("MAX_RUN_TIME", 5.minutes)
end
instead of
Delayed::Job.destroy_failed_jobs = false
silence_warnings do
Delayed::Job.const_set("MAX_ATTEMPTS", 3)
Delayed::Job.const_set("MAX_RUN_TIME", 5.minutes)
end
Awesome date time plugin
After a long time, I have seen such a great datetime picker for rails. “Awesome” is just not the word for it
Ruby Class with Mass assignable attributes
Well, this is very weired again, but it seems ruby class does not allow me to have mass assignable attributes as you could do with the ActiveRecord models using attr_accessible or attr_accessor.
Its very painful to have too many getters and setters in the class and to assign them manually when you create an object of that class or to have constructor which has those hundreds of params.
So, how do we achieve that. Well there are two ways.
1. The hacky way:
class Woo
attr_accessor :f_name, :l_name
def initialize(params)
#instance_eval &block
params.each do |key, value|
eval("self.#{key}= '#{value}'") if self.respond_to? "#{key}="
end
end
end
woo = Woo.new(:f_name => "first_name", :l_name => "last_name")
What we are doing here is that we are trying to assign these instance variables in an eval loop. So the constructor to the method just takes all your parameter hash and then tries to see if your class responds to that method setter (which it should as you have defined the att_accessors for those instance variable already), and then assigns them the corresponding value from the hash.
2. The cleaner way, but which hides what you have inside as attributes
require 'ostruct'
Class woo < OpenStruct
end
woo = Woo.new(:f_name => "first_name", :l_name => "last_name", any_no_of_craps...)
OpenStruct is nothing but another container like a hash in ruby which just gets better when it comes to serialization of these objects (which in my own case is a need)
Hope that helps a bit to someone!!
no such file to load — capistrano-ext
If you are using capistrano and capistrano-ext together, please note that the following line in you environment.rb might not be sufficient:
config.gem “capistrano”
config.gem ‘capistrano-ext’
In this case, you might get the error :
no such file to load — capistrano-ext
bla-bla-bla
All you need for your app to find the gem (I am assuming that you have already installed the gem by running the command : rake gem install capistrano-ext) is following:
config.gem “capistrano”
config.gem ‘capistrano-ext’, :lib => ‘capistrano’
Mocha should be loaded after Shoulda with rails 2.3
Well, this one really drove me crazy. So I decided to finally pen it down. May this help somebody else in future. This might not be very new, but really crazy stuff.
If you are using Mocha and Shoulda both, then please load Mocha after you have loaded Shoulda. Like following:
config.gem “shoulda”, :lib => “shoulda”, :source => “http://gems.github.com”, :version => “>=2.11.3″
config.gem ‘mocha’
If you load Mocha before Shoulda, what happens is that Mocha gets crazy with the expectations. You might see your tests failing because of some weird expectations that are not set in the current failing test but were set somewhere else in some other test.
So, in a nut shell, Mocha starts keeping the expectations across tests instead of wiping them off after each test, as it is expected to do.
