Visualizzazione post con etichetta ruby on rails. Mostra tutti i post
Visualizzazione post con etichetta ruby on rails. Mostra tutti i post

venerdì 29 aprile 2011

jQuery on Rails 3

Ruby on Rails is an open source web application framework for the Ruby programming language. The last version of this framework is the 3.0.0 released Aug 29, 2010. By default, when you start a new Rails application with:
rails new my-app
by default it uses prototype as javascript framework. Now prototype isn't the best solution for all developers so you might want to use jQuery instead.
To use jQuery you need to create a new rails application with the following command instead:
rails new my-app -J
notice the "-J" option that skips the creation of the prototype files in the new app.
Now you have to download jQuery from its website and put the jquery-x.x.x.js file inside the public/javascripts folder of your application.
Now download the file rails.js from http://github.com/rails/jquery-ujs and put it inside the public/javascripts folder too.
Now you need to add a reference to jQuery inside the application layout. To do so, open the file app/views/layouts/application.html.erb and add the following code:
<%= stylesheet_link_tag 'jquery-ui-1.8.5', 'scaffold' %>
<%= javascript_include_tag 'jquery-1.4.3', 'jquery-ui-1.8.5', 'application' %>
You're done! Now you can use Ruby on Rails with jQuery.
Happy coding!

martedì 26 aprile 2011

How to start/stop/restart delayed_jobs from the command line

Delayed Jobs is a background processing tool written in ruby. When you use it inside a Ruby on Rails application, usually you start it with:
rake jobs:work
If you need to start delayed jobs from a Capistrano task or from a monitoring framework like God, you should use the following commands.
To start:
script/delayed_job start
To stop:
script/delayed_job stop
To restart:
script/delayed_job restart

Hope this helps.

martedì 14 dicembre 2010

How to send a dinamically generated file in a Rails 3 application

Sometimes can be useful to send a file to the user instead of rendering a page. When you have a fixed location for the file you simply achieve this with a link to the file inside the view, but there may be some cases in wich you have to dinamically generate the file to send. To do this you have to add a new custom action to the controller in wich you want to add the download.
In this example I suppose to have a simple blog application where the user can download the content of a post in text format. So, in the posts controller we add the following action:
class PostsController < ApplicationController

...

def download
  @post = Post.find(params[:id])
  send_data @post.body, :filename => @post.title
end

...

end
After this we have to add a new line into our config/routes.rb file to make this action accessible with a get request:
...
resources :posts do
  member do
    get 'download'
  end
end
...
Now we can add a simple link in our view:
<%= link_to 'Download', download_post_path(@post), :title => 'Download post' %>
Here we suppose to have the instance variable @post created somewhere (i.e. in the show action).
Now we can display our page and download the post as a text file.
Hope this helps, have fun with Ruby on Rails!

giovedì 11 novembre 2010

Case insensitive uniqueness validation in rails 3

The default uniqueness validation (using sexy validations) in Rails 3 is case sensitive. This means that if you wrote the following class, with the following validation:
Class User < ActiveRecord::Base

  validates :username, :presence => true, :uniqueness => true

end
these usernames will pass the validation and will be stored in the database:
foo, Foo, fOo, fOO, ...
To avoid this problem you should make a change to the previous model in order to make the uniqueness validation case insensitive. To acheive this you have to pass an hash of parameters to the uniqueness validator. The new code may look like this:
Class User < ActiveRecord::Base

  validates :username, :presence => true, :uniqueness => {:case_sensitive => false}

end
Hope this helps :)

giovedì 28 ottobre 2010

Install Ruby Version Manager (RVM) on Ubuntu Lucid/Maverick

RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems. In order to install RVM you need a few packages:
sudo apt-get install build-essential curl zlib1g-dev
libreadline5-dev libssl-dev libxml2-dev
Now you need to install a temporary version of ruby and rubygems. This version will be used to install the rvm gem.
sudo apt-get install ruby1.8 rubygems1.8
After the installation is completed you can install the rvm gem with the command:
sudo gem install rvm
To complete the installation now run:
/var/lib/gems/1.8/bin/rvm-install
As the last step you have to add the following line to the file .bashrc in your home. So edit it with your favourite editor or create it if it doesn't exist.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.
To verify the installation, type rvm -v and the output should be the following:
$ rvm -v
rvm 1.0.15 by Wayne E. Seguin (wayneeseguin@gmail.com) [http://rvm.beginrescueend.com/]
Now to install ruby inside rvm use the simple command:
$ rvm install 1.9.2
"1.9.2" is the version of the ruby interpreter that you want to install. RVM automatically downloads the sources from the internet, compiles and install them. To set 1.9.2 as the default version use the command:
rvm --default 1.9.2
Enjoy with ruby!

sabato 23 ottobre 2010

Installing Aptana Studio 3.0 on Ubuntu Lucid/Maverick

Aptana Studio is an open source integrated development environment (IDE) for building Ajax web applications. It includes support for JavaScript, HTML, DOM, and CSS with code-completion, outlining, JavaScript debugging, error and warning notification and integrated documentation.
If you install the plugin version of the Aptana Studio IDE on an existing installation of eclipse done by the official ubuntu repository, the installation procedure may not work. To solve this problem you must install a few packages through the command line or through Synaptic. The packages you need to complete the installation of the plugin are:
  • eclipse-plugin-cvs
  • eclipse-jdt
  • eclipse-pde
So, the command you should give is:
sudo apt-get install eclipse-plugin-cvs eclipse-jdt eclipse-pde
After this you can continue the installation. When the installer tells you that there may be software from unsafe software sources, answer yes to finish the installation.
At the end you should restart Eclipse and you're done!