Tuesday, October 20, 2009

Ralis 2.2 with mysql Resolved errors

When you work in Rails 2.2 , with mysql you may get following error after installing mysql driver and trying to run the project.

!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install
the mysql gem and try again: gem install mysql.
rake aborted!
126: The specified module could not be found. - C:/Ruby/lib/ruby/gems/1.8/gems
/mysql-2.8.1-x86-mswin32/lib/1.8/mysql_api.so

The problem for above error is, MySQL 5.1 client library doesn't play well with Rails.

Solution is

  • copy the downloaded file to C:\Ruby\bin (or wherever you installed Ruby)

  • restart MySQL server

Wednesday, October 14, 2009

Rails Set the Index Page

Follow the steps given bellow
1. Delete public/index.html
2. Add 'map.root :controller => ""' to routes.rb
3. and then of course create the controller with the action index.

Java Script Validations in RESTful Ruby on Rails Form

We can validate ruby on rails form using java script.It is show in bellow example

java script function

function validate()
{
//validation code
if (valid)
return true
else
return false
}

ruby on rails form

<% form_for(@patient_details,:html=>{:onsubmit=>"return validate();"}) do |f| %>
<%= f.error_messages %>
#form

<%end%>

Switch statement in ruby on rails

In other languages switch case statement use for avoiding many if else conditions. In ruby also use these type of tack-tick for avoid many if else conditions.

try this code

exapmle 1
case name
when "banusha"
#some_code
when "kamal"
#some_code
else
#...
end

example 2

case number
when 1
puts "number is equal to 1"
when 2..9
puts "number is between 2 and 9"
when 10
puts "number is equal to 10"
end

example 3

case number
when 1 then puts "number is equal to 1"
when 2..9 then puts "number is between 2 and 9"
when 10 then puts "number is equal to 10"
end
example 4

case number
when 1 ; puts "number is equal to 1"
when 2..9 ; puts "number is between 2 and 9"
when 10 ; puts "number is equal to 10"
end


Ruby on Rails -try catch fainally block

Ruby On Rails in more human readable language than other languages. So ruby use try catch finally block also human readable form as follows.begin,rescue,ensure same as try catch finally

begin
somecode()
rescue

puts "error"
ensure

all_ways_execute_code
end

Acts_as_Authenticated Plugin in Rails

In ruby on rails environment by using this plugin we can built a login system with all most all the function, with in few minitues. Follow the steps in bellow

1.Install the plugin: script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
2. Generate the user and account models: script/generate authenticated user account
3. Rake the DB to add the users table: rake db:migrate
4. Finally, add this line to application.rb controller so you can use the gem: include AuthenticatedSystem

Type Url -http://localhost:3000/account/signup
Then you can see the sign up page
Type- http://localhost:3000/account/login
then you can see tha login page

To protect pages so they require login, simply add this line to the beginning of your controller class:

before_filter :login_required

If you want to exclude certain views from requiring authentication, just add the list of exceptions:

before_filter :login_required, :except => [:show]

def show
# view stuff ...
end