Showing posts with label ubuntu. Show all posts
Showing posts with label ubuntu. Show all posts

Sunday, February 10, 2013

Sublime Text 3 Quick Install and Setup for Rails on Linux

Sublime Text 3 Beta is now open to non-registered users (27 June 2013).
Sublime Package Control for ST3 is currently in Alpha.

Install ST3


Download latest deb build from http://www.sublimetext.com/3
Install (double click) then copy / paste your licence
If you have a conflict with ST2 then remove it (see below) or download the tarball

sudo apt-get remove --purge sublime-text-2
sudo apt-get autoremove

Install Package Control

http://wbond.net/sublime_packages/package_control/installation#ST3

cd /home/username/.config/sublime-text-3/Packages
git clone https://github.com/wbond/sublime_package_control.git "Package Control"
cd "Package Control"
git checkout python3


Add some Basic User Settings (adjust according to your preferences)
Preferences > Settings - User
{
"color_scheme": "Packages/RailsCasts Colour Scheme/RailsCastsColorScheme.tmTheme",
"detect_indentation": false,
"font_face": "Consolas",
"font_size": 13,
"ignored_packages":
[
"Vintage"
],
"scroll_past_end": false,
"tab_size": 2,
"translate_tabs_to_spaces": true,
"trim_trailing_white_space_on_save": true,
"word_wrap": true
}

Use Package Control to install a Theme
Popular packages are: RailsCasts Colour Scheme, Theme - Soda, Solarized Color Scheme

Update your Preferences > Settings - User to swap
Use Package Control to install syntax highlighting
Haml, Sass, Less, Coffeescript select them as defaults for the corresponding filetypes (click on the current filetype at the bottom right of ST3 window).

ERB Insert and Toggle Commands
https://github.com/eddorre/SublimeERB

Add to User Keybinding File
[
  { "keys": ["ctrl+shift+."], "command": "erb" }
]

Run Tests in the console
Install RubyTest https://github.com/maltize/sublime-text-2-ruby-tests

You must run Sublime Text 3 from the command line (subl)

control shift t Run all tests in the file
control shift e Run last test

There are issues with Colour output and esc characters - you may prefer to use the linux terminal

Create a plugin to Open file in browser
Tools > New plugin...

Copy code below / paste / save as OpenBrowser.py
# OpenBrowser.py
import sublime, sublime_plugin
import webbrowser

class OpenBrowserCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      url = self.view.file_name()
      webbrowser.open_new(url)

Add to User Keybindings to Open file in new browserPreferences > Key Bindings - User

Copy code below / paste / save
[
  { "keys": ["ctrl+shift+b"], "command": "open_browser" }
]

Monday, June 4, 2012

PostgreSQL setup for Rails development in Ubuntu 12.04

Rails comes with SQLite and it works out of the box with no configuration. That's great! but... its not suitable for a production environment so we need another database if your app is to see the light of day.

You could use SQLite in your development environment and switch it up for production, however, it makes far more sense to use the same database in development and production.

My personal preference is to use PostrgreSQL, its open source and gaining traction in the Rails world due to stability and rather nifty features. Heroku also uses PostgreSQL so its a good option if you plan on using their services.

See the excellent PostgreSQL Documentation for more information.

1. Install postgresql and admin tools through the package manager


sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3

Change the postgresql configuration to solve this error
FATAL: Peer authentication failed for user

sudo nano /etc/postgresql/9.1/main/pg_hba.conf

Replace local peer with local md5 in the file. Then reload postgresql to apply the change.

sudo /etc/init.d/postgresql reload


2. Setup your databases from the command line


a) Login to postgresql prompt as the postgres user

sudo su postgres -c psql

b) Create a postgresql user for your project

create user username with password 'password';

TIP: If you are running tests, then to ease permission issues setup your postgres user with the same name and password as your Ubuntu user and make him a postgres superuser ( \du lists users with permissions).

alter user username superuser;


c) Create the development and test databases

create database projectname_development;
create database projectname_test;

d) Give permissions to the user on the databases

grant all privileges on database projectname_development to username;
grant all privileges on database projectname_test to username;

e) Finish your postgresql session

\q

Other useful commands in a postgresql session


Update password

alter user username with password ‘new password’;

Destroy database

drop database projectname;

Connect to specific database

\c databasename

Common MySQL commands with postgresql shortcut

MySQL Postresql
SHOW DATABASES \l
SHOW TABLES \d
SHOW COLUMNS \d table



3. Place a database configuration file in your rails project


Save the code below as app/config/database.yml and rake db:migrate your project

development:
  adapter: postgresql
  encoding: unicode
  database: projectname_development
  pool: 5
  username: username
  password: password

test:
  adapter: postgresql
  encoding: unicode
  database: projectname_test
  pool: 5
  username: username
  password: password




4. Misc Errors encountered


If you are importing data (or using a seed file) then you may run across this error.
PG::Error: ERROR: duplicate key value violates unique constraint "table_name_pkey"

Your import didn’t use the sequence so we need to reset it to be in sync again.

Note when replacing table_name use a plural.

sudo su postgres -c psql
\c database_name
select setval('table_name_id_seq', (select max(id) + 1 from table_name));