Chee Chong Hwa aka CCH, a Malaysian Chief Software Architect blogs on his experiences with Ruby on Rail running on Win2003 Server+Apache 2.26 environment since 2007. Flirted briefly with Ubuntu 9.04 in 2009. Since January 2017, now using RubyMine IDE/Notepad++ to work with Ruby 2.26 +Rails 5.01+Apache 2.4.23 + MySQL 5.7.14 on Windows 10 64-bits.
Monday, January 30, 2017
How to set the Default Page in Rails 5
Steps
Step 1: Add a root route
Open the fileconfig/routes.rb
in an editor (eg NotePad++)Look for the lineRails.application.routes.draw
at the beginning of the file, and add the lineroot 'topics#index'
after it. When you are done the start of the file should look like this:Rails.application.routes.draw do root 'topics#index'
Step 2: Confirm your changes
Go back to http://localhost:3000/. You should be taken to the topics list automatically.
More at http://docs.railsbridge.org/intro-to-rails/setting_the_default_page
Sunday, January 29, 2017
Role-based Authorisation in Rails
Simply add
access-granted
to your Gemfile:gem 'access-granted', '~> 1.0.0'
then bundle and run the generator to create your starter AccessPolicy:
rails generate access_granted:policy
which you can find in
app/policies/access_policy.rb
.
Forums are a good showcase of user hierarchy I mentioned above, because they usually need a hierarchical permission system based on the following roles:
- moderators
- registered users
- guests
The order in which roles are sorted is also the order of importance. The moderator is the role with the most permissions and the guest is the role with the least.
In Access Granted roles are defined in the same, top-to-bottom, order:
class AccessPolicy
include AccessGranted::Policy
def configure
role :moderator, proc { |user| user.moderator? } do
# permissions will go here
end
role :member, proc { |user| user.registered? } do
# permissions will go here
end
role :guest do
# permissions will go here
end
end
end
Every role has a name and an optional predicate to check if it’s active for the user.
Note: Above you can see me using
.moderator?
and .registered?
methods inside the Procs - these are app-specific and may come from, for example, ActiveRecord::Enum module.More at
https://blog.chaps.io/2015/11/13/role-based-authorization-in-rails.html
How to Add a new column to an existing table via db:migrate
Use this command at rails console
rails generate migration add_fieldname_to_tablename fieldname:string
and
rake db:migrate
to run this migration
You can also do
rake db:rollback
if you have not added any data to the tables.Then edit the migration file by adding the email column to it and then call
rake db:migrate
This will work if you have rails 3.1 onwards installed in your system.
Much simpler way of doing it is change let the change in migration file be as it is. use
$rake db:migrate:redo
.
This will roll back the last migration and migrate it again.
Saturday, January 28, 2017
How to create a Rails Project with SQL Server Adapter
gem install rails -v 4.2.7.1
rails _4.2.7.1_ new MYAPP --database=sqlserver
Database.yml
adapter: sqlserver mode: dblib host: 127.0.0.1 port: "1433" username: sa password: Password1337 database: test timeout: 20
rails _4.2.7.1_ new MYAPP --database=sqlserver
Database.yml
adapter: sqlserver mode: dblib host: 127.0.0.1 port: "1433" username: sa password: Password1337 database: test timeout: 20
SQL Server Rails Database.Yml
The corresponding
/config/database.yml
file should be as followsdevelopment:
adapter: sqlserver
host: 127.0.0.1 # used when dataserver is blank
port: 1433
database: database_name
username: sa
password: billgates
Note: you are supposed to create the database in SQL Server
Friday, January 27, 2017
How to use Active_Scaffold with Rails (Updated 19th Feb 2017)
To get started with a new Rails project
Added to Gemfile
gem 'active_scaffold'
gem 'active_scaffold', github: 'activescaffold/active_scaffold', branch: 'master'
Run the following commands
bundle install
rails g active_scaffold:install
bundle exec rake db:create
rails g active_scaffold:resource User name:string
bundle exec rake db:migrate
Nb. In Rails5, you can use rails db:create and db:migrate
Run the app and visit localhost:3000/users
See also https://vhochstein.wordpress.com/2010/08/28/setup-activescaffold-rails-3/
and http://vinsol.com/blog/2010/04/09/introduction-to-active-scaffold-part-i/
Run the app and visit localhost:3000/users
See also https://vhochstein.wordpress.com/2010/08/28/setup-activescaffold-rails-3/
and http://vinsol.com/blog/2010/04/09/introduction-to-active-scaffold-part-i/
How to run Rails in terminal mode ?
C:\RailsProjects\pms4mysql>rails s
=> Booting Puma
=> Rails 5.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.6.2 (ruby 2.2.6-p396), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
=> Booting Puma
=> Rails 5.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 3.6.2 (ruby 2.2.6-p396), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Which Version of Ruby and Rails ?
I still have a CRM application running 24 x7 x 365 since 2007 using Ruby 1.86 and Rails 1.25 combination running on a Windows 2003 Server :-) I then used the Aptana IDE for which development was discontinued a few years ago.
Going back to Rails, I discovered RubyMine 2016.3 and is using it with RailsInstaller Windows Ruby 2.2 distribution.
Packages included are
Going back to Rails, I discovered RubyMine 2016.3 and is using it with RailsInstaller Windows Ruby 2.2 distribution.
Packages included are
Wednesday, January 18, 2017
active_scaffold 3.4.38
gem 'active_scaffold', github: 'activescaffold/active_scaffold', branch: 'master'
RUNTIME DEPENDENCIES (1):
DEVELOPMENT DEPENDENCIES (3):
Subscribe to:
Posts (Atom)
Welcome to Rails.. Rails... Rails !
In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.
As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam
CCH
10th June 2010, 19:42
As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam
CCH
10th June 2010, 19:42