MarkItUp: Rails plugin that turns any textarea into a markup editor

I recently published a plugin that helps you turn any textarea into a markup editor. It is based on excellent markItUp! jQuery plugin.

Example

The most simple usage with preset defaults

<html>
<head>
  <%= javascript_include_tag "path/to/jquery" %>
  <%= mark_it_up '#miu_test' %>
</head>
<body>
  <%= form_tag do %>
    <%= text_area_tag "miu_test" %>
  <% end %>
</body>
</html>

You can see a lot more cool examples in action on markitup.cingel.hr.

More info

markitup.cingel.hr
github.com/cingel/mark_it_up

Simple Search Rails plugin

SimpleSearch brings simple search to ActiveRecord. It ads simple_search named scope that accepts query as parameter.

The idea is that you provide the query and plugin does the rest (splits query to keywords and compose where statement).

This can be very useful in case you just want to filter list of records by some query, you have autocomplete field, … or something similar.

Continue reading

Create new app with specific Rails version

I recently installed Rails 3 beta release but I needed to create new app with the 2.3.5. version. I was searching for a way to do that and I found this

  rails _2.3.5_ appname

Unfortunately this will produce an error similar to this

  /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:827:in `report_activate_error': RubyGem version error: railties(3.0.0.beta not = 2.3.5) (Gem::LoadError)
	from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:261:in `activate'
	from /usr/local/lib/ruby/site_ruby/1.8/rubygems.rb:68:in `gem'
	from /usr/local/bin/rails3:18

To fix this first run

Continue reading

Ruby library for parsing, validating and formatting phone numbers

Tomislav Car has just released Phone, Ruby library for phone number parsing, validation and formatting. It should save you a lot of time if you need any of the following:

* you have area where users input phone numbers in many different formats
* output phone numbers in specific format
* you need to send SMS messages from your app
* …

Continue reading

Rails, CSRF and Ajax requests

Rails protects controller actions from CSRF (Cross-Site Request Forgery) attacks with a token based on a random string stored in the session. The token parameter is named authenticity_token by default and will be embedded in all forms and Ajax requests generated by Rails.

You should also add this token to all Ajax request that you hand coded. As suggested in Rails documentation you can add this line in head section.

  <%= javascript_tag "window._token = '#{form_authenticity_token}'" %>;

and then add authenticity_token to parameters option of Ajax requests

  new Ajax.Request('/some/url', {
    parameters: "foo=bar&authenticity_token="+_token
  });

Remote forgery protection plugin

This can get tedious if you have a lot of Ajax requests so I wrote a simple plugin that adds authenticity token to all Ajax requests automatically.

You can install it with

  script/plugin install git://github.com/vlado/remote_forgery_protection.git

Now all you have to do is add this line inside head section of you’re layout

  <%= remote_forgery_protection %>

and all non GET Ajax request will have authenticity_token parameter automatically included.

Continue reading

Radiant CMS, disable caching in development

I am working on extension for Radiant CMS. I wanted to disable caching for development, but wasn’t able to find right method for this, so finally I did it by putting this into my extensions activate method

if RAILS_ENV=="development"
  Page.class_eval {
    def cache?
      false
    end
  }
end

This is more of a hack then a real solution, so please use comments to point me in the right direction. I’m using Radiant version 0.8.1.

Disabeling sessions in Rails

The simplest way to disable sessions in Rails is to use session :o ff (see ActionController::SessionManagement::ClassMethods).

session :o ff

To disable session suport only for a specific controller add session :o ff to that controller

class MyController < ApplicationController
    session :o ff
end

Written like that, sessions are disabled for all actions on this controller.

Like filters, you can specify :o nly and :except clauses to restrict subset. The following code will disable session for first_action and third_action, but not for second_action.

class MyController < ApplicationController
  session :o ff, :o nly %w(first_action third_action)

  def first_action
  end

  def second_action
  end

  def third_action
  end
end

Continue reading

Determining the number of days in a month

Here is a simple method that returns number of days for wanted month and year. If year is ommited current year is selected.

def month_days(month, year=Date.today.year)
  mdays = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
  mdays[2] = 29 if Date.leap?(year)mdays[month]
end

Simple examples:

month_days(1)       => 31
month_days(2, 2007) => 28
month_days(2, 2008) => 29

Now, let’s assume we have a date from a database, and we want to find number of days in a month from that date.

month_days(date.month, date.year)

But with two more lines of code

Continue reading

Quick Start with Rails on OS X

Install Ruby

OS X (10.4) comes with Ruby 1.8.2 already installed.
For a quick start this is OK so let’s install Rails.

Install Rails

Since Rails comes as gem package, first we need to install RubyGems.
Download the latest package from RubyForge and unzip it.
Now go to the directory where you unpacked the package and run

$ sudo ruby setup.rb

When asked enter your password, and after install finish you can verify that everything went well by typing:

$ gem -v

Output should look something like this, depending on version you installed

=> 0.9.4

Now installing Rails is as simple as

$ sudo gem install rails --include-dependencies

To verify installation, type:

$ rails -v
=> Rails 1.2.3

Install MySQL

You can skip this step if you don’t want to use database but since Rails is a framework for developing database-backed web applications, you’ll probably want to use one.

  1. Download the MySQL package for OS X for your platform
  2. Double-click the drive image to mount it
  3. Locate the MySQL installer (something like mysql-5.0.45-osx10.4-i686.pkg) and run it, authenticating as needed
  4. Double-click MySQLStartupItem.pkg
  5. Double-click MySQL.prefPane and install it

Once the install is complete, start the MySQL server using the control panel. ( System Preferences -> Other -> MySQL ) and check Automatically Start MySQL Server on Startup

Test your installation

To start a new project (application), go to the directory where you plan to keep your applications (i.e. ~/Apps) and execute following commands

$ rails test_app
$ cd test_app
$ ./script/server

This will generate Rails skeleton in ~/Apps/test_app, and start a WEBrick server on port 3000.
Now open your browser and enter http://localhost:3000 in address bar. You should see a page with “Welcome aboard, You’re riding the Rails!” message.

We also need to tell Rails which database to use.
To do this edit database.yml file (~/Apps/test_app/config/database.yml) to look like this

development:
  adapter: mysql
  database: test_app_development
  username: root
  password:
  host: localhost

test:
  adapter: mysql
  database: test_app_test
  username: root
  password:
  host: localhost

production:
  adapter: mysql
  database: test_app_production
  username: root
  password:
  host: localhost

Naturally we also need to create a database

$ mysqladmin -u root create test_app_development

and table in that database

$ mysql -u root

mysql> create table items (
         id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
         name VARCHAR(100) );

Execute scaffold command

$ script/generate scaffold item

Open http://localhost:3000/items and if you see something like this

Listing items

Name

New item

you have everything needed to get started with Rails development so Enjoy!

Technorati Tags: , , ,