Archive for the ·

Programming

· Category...

If your Ajaxed form field disappears during form submission

no comments

I spent half a day breaking my head with this, so this might help someone.

I had two select boxes inside a HTML form, selecting one updates the values in the other select box.  I used Ajax and a server side call to do this updation, which works fine.  But when I submit the form, the new select tag is missing in the form parameters.

If you run into a situation like this, the answer may lie in your HTML form.

My code did something like this:

<table>
  <form>
    <tr>
      <td>
        <div id="foo">
        </div>
      </td>
    </tr>
   </form>
</table>

Notice the <form> inside the <table>?  That was the culprit.

Chrome developer console dumped this message which helped.

Once I flipped the order and moved the <form> to encapsulate the <table>, that fixed the bug :) .

Migrating a rails app from Rails 2.3.8 to Rails 3.0.0 Beta 4

no comments

I recently migrated a Rails 2.3.8 app to Rails 3.0.0Beta4. Here is what I learned through that exercise.

1) Install rails

sudo gem install rails --pre

2) Install rails upgrade helper plugin

rails plugin install git://github.com/rails/rails_upgrade.git
script/plugin install git://github.com/rails/rails_upgrade.git
/Library/Ruby/Site/1.8/rubygems.rb:233:in `activate': can't activate rails (= 2.3.8, runtime) for [], already activated rails-3.0.0.beta4 for [] (Gem::LoadError)

Couldn’t proceed with that error. At this point, after spending an hour, I gave up on trying to move my existing Rails2.x app as is to Rails3 since it is clear that the boot process is different for Rails3 and keeping the existing boot.rb etc will create more chaos.

3) Create a new rails 3.0.0 application

rails new app_30
cd app_30

Copy over app/controllers, app/views, app/lib, app/models, config/database.yml from old to new

4) Start server script/rails server (script/server won’t work)

5) Add gem ‘mysql’, ’2.8.1′ to gemfile

6) Set the root correctly in routes.rb file. For some reason it doesn’t find default controller.

root :to => "nav#index"

7) Install all the missing gems and add them to gemfile with appropriate versions, if need be. If you have legacy plugins, the newer gems don’t seem to work correctly, stick to the legacy plugins and move them over to the new app.

8) paginate_by_sql breaks since add_limit! is removed from ActiveRecord::Base. Fix paginate by sql to do this:

module ActiveRecord
class Base
  def self.find_by_sql_with_limit(sql, offset, limit)
   sql = sanitize_sql(sql)
   find_by_sql(sql + " LIMIT #{offset},#{limit}")
  end

  def self.count_by_sql_wrapping_select_query(sql)
     sql = sanitize_sql(sql)
   count_by_sql("select count(*) from (#{sql}) as x")
  end
end
end

9) Add the following two routes to routes.rb. Without these two routes /controller/action is not recognized by default.

# Install the default route as the lowest priority.

map.connect ':controller/:action/:id.:format'

map.connect ':controller/:action/:id'

10) ActionDispatch::Cookies::CookieOverflow error. Set your session_store to be active_record_store. Change this in config/initializers/session_store.rb

Rails.application.config.session_store :active_record_store, :key => '_mygreatapp_30_session'
11) Rails generated HTML tags are HTML escaped by default. So if you did something like

<%=link_to “&nbsp;foobar” … %>, the &nbsp; would be escaped and represented literally on screen.


Likewise, if you had code that did

<%= getImage %> and getImage helper returned “<a href=”…”><img src=””/></a>, the returned string would be escaped. Use <%= image_tag(getImage) %> and change the getImage to return just the image path.

12) If you are sure that the code is returning safe HTML, then do this:

<%= helper_returning_html.html_safe %>

or

<%= raw helper_returning_html %>

Blog changes done and sorry for the duplicates

no comments

I am done with all the blog changes I talked about in the previous post. The photoblog now exists under the unified blog. I also mirrored the two domains and then took it to the next level by switching the domains as well.

http://blog.anands.net is now the unified blog URL and http://textblog.anands.net mirrors it. None of you have to change or update any of the RSS feeds, they should work automagically.

Getting this to work without breaking (any more) of the existing clients and permalinks out there was fun. I learned way more about WordPress than I ever needed to :) . I have a couple of blog entries on how to migrate from PixelPost to WordPress and how to move WordPress from one sub-domain to another.

Sorry for any of the duplicate posts in your RSS feeds, I could have avoided them with a little bit more careful planning, but I wasn’t thinking it through.