Skip to content

Instantly share code, notes, and snippets.

@vijaydev
Created November 21, 2010 18:41
Show Gist options
  • Save vijaydev/708995 to your computer and use it in GitHub Desktop.
Save vijaydev/708995 to your computer and use it in GitHub Desktop.
Rails
Rails 3 no longer auto loads the lib folder. We can make it auto load the lib folder by putting it in config/application.rb like thus: config.autoload_paths += %W(#{config.root}/lib)
All strings are HTML escaped by default in Rails 3. Need to call html_safe on strings which we know are sure to be safe to display the HTML properly.
Reserved words: "config", "process"
Specifying RailsEnv in Apache configuration (vhost/httpd.conf etc) for Passenger 3.0.0 does not work in Rails 3. Changing that to RakeEnv does the trick. This was fixed in Passenger 3.0.1.
To add the default integer primary key to a table which was created with :id => false setting, we can do add_column :table_name, :id, :primary_key
Rails.public_path gives the path to public folder in the app.
Rails.configuration.paths can be used to obtain the paths of various folders in the app. eg. Rails.configuration.paths.app.views.paths gives ["#{Rails.root}/app/views"]
https://github.com/rails/jquery-ujs - When using this to add jquery to the app, run the generator for jquery-ui if needed like this: rails g jquery:install --ui. If this is not done, jquery-ui.js will not be loaded when :defaults is called by the javascript_include_tag.
errors.add - replace in R3 by - errors.add_to_base
Check simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/ for ajax usage
Customize json output like this:
def to_json(options = {})
Exam.include_root_in_json = false
super(options.merge(:only => [ :id, :name, :term, :creator_id ]))
end
Using render :json => @exams # This will call the customized version (if exist) only if the @exams is an Array. Not an ActiveRecord::Relation
http://seanbehan.com/rails-source/how-to-get-a-list-of-all-registered-mime-types-in-rails/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment