Saturday, June 21, 2014

Mount Amazon EC2 instances on OS X with SSHFS

This gem lets you manipulate files on your remote EC2 instance as if they were local.  Think of it like a mini Dropbox near instantaneously updating files between your MacBook and your Ubuntu instance at Amazon.

Step 1: Configure SSH

Copy your Amazon .pem file to ~/.ssh
Change permissions on the .pem file:

chmod 600 ~/.ssh/myfile.pem

Test the .pem file:

ssh -i ~/.ssh/myfile.pem ubuntu@aws_instance_name.com

Store the .pem file in ssh so you can enter a shorter ssh command in the future:

ssh-add -K ~/.ssh/myfile.pem

Now you can ssh to your Ubuntu instance more easily:

ssh ubuntu@aws_instance_name.com

Step 2: Install SSHFS

Download and install both packages from http://osxfuse.github.io

OSXFUSE
SSHFS

Step 3: Configure the mount point

mkdir ~/AmazonEC2

sshfs ubuntu@aws_instance_name.com:/home/ubuntu/rails_projects ~/AmazonEC2

Now your EC2 files located at /home/ubuntu/rails_projects will be "synced" with ~/AmazonEC2

I do not think they are actually syncing - based on response times, I think file manipulations are done directly to the remote host.

For fun, take a look at mounted file systems:

df


Thursday, January 9, 2014

Turn off wordwrap in Vim

Turns off wordwrap in Vim --

:set nowrap

Turns it back on --

:set wrap


Tuesday, December 31, 2013

Quick simple Rails form

Here's a very quick and very simple Rails form.  I am pulling this from this Youtube tutorial, but documenting the actual steps needed because I find it easier to read and newer versions of Rails require that we do things a little differently than what is described in the video.

cd rails_projects
rails new simpleform
cd simpleform
rails g controller students
rails g model Student firstname:string lastname:string
rake db:migrate
cd db
sqlite3 development.sqlite3
.schema
.quit
cd ..

Change app/controllers/students_controller.rb from this:

class StudentsController < ApplicationController
end




To this:

class StudentsController < ApplicationController
  def new
    @student = Student.new
  end

  def create
    @student = Student.new(params[:student])
    if @student.save
      redirect_to new_student_path
    end
  end
end




Create new file app/views/students/new.html.erb:

Enter new student information
<hr>
<%= form_for @student do |f| -%>
  Firstname: <%= f.text_field :firstname %><br />
  Lastname: <%= f.text_field :lastname %><br />
  <%= f.submit %>
<% end -%>


Insert the resources line into config/routes.rb:

Simpleform::Application.routes.draw do
 

  resources :students



Start up the rails server to see how things look thus far:
rails s

Point your browser to http://localhost:3000/students/new

This shows you a simple form with text fields for firstname and lastname.  Enter in some text for the two fields and click the submit button.  You will get an error:

ActiveModel::ForbiddenAttributesError in StudentsController#create
ActiveModel::ForbiddenAttributesError
Extracted source (around line #7):
@student = Student.new(params[:student])

I think it has to do with security and probably a change that happened in Rails versions. 

Here's the fix.

Change app/controllers/students_controller.rb:

class StudentsController < ApplicationController
  def new
    @student = Student.new
  end

  def create
    @student = Student.new(user_params)
    if @student.save
      redirect_to new_student_path
    end
  end

  private

   def user_params
     params.require(:student).permit(:lastname, :firstname)
   end


end



Now point your browser to http://localhost:3000/students/new
and try to enter a couple records.

We can view the new records in our database:

cd db
sqlite3 development.sqlite3
.schema
select * from students;
.quit
cd ..



So we are successfully inserting data into our database!  Hurray!

Now we will make a couple changes so our web page not only gathers student names, but displays them as well.

Append the following lines to app/views/students/new.html.erb:

<hr>
Display all students' information<br />
<% if !@students.blank? %>
  <% for item in @students %>
    <%= item.firstname %> <%= item.lastname %> <br />
  <% end %>
<% else %>
<% end %>



We also have to define @students in app/controllers/students_controller.rb:

  def new
    @student = Student.new
    @students = Student.find(:all)
  end


Rails Tutorial using mysql

So I am reading this Rails Tutorial a second time, this time doing some off-roading.

[Edit 2015-12-15 - Reader "Zane" was kind enough to let me know the link was busted. Looks like a new edition was released and can be seen here.  Another tutorial can be viewed here.]

To start a project using mysql (instead of mysqlite), do this:

rails new app_name -d mysql

I am running OSX with a mysql image downloaded from Oracle.  When I tried to run this command:

rails generate scaffold User name:string email:string

The command failed with:
Library not loaded: libmysqlclient.18.dylib (LoadError)

Here is the fix:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

The tutorial next has us run:

bundle exec rake db:migrate

Which results in this error:

rake aborted!
Access denied for user 'root'@'localhost' (using password: NO)

The fix is to edit your config/database.yml file:

Scroll down and replace update username: and password: values appropriately.

Re-running the command above yields:

rake aborted!
Unknown database 'app_name_development'

The fix is to create a new database:

rake db:create

Now the bundle exec command attempted above will work.

We can confirm this worked by interacting with the demo app and running the following commands:

mysql -u root -p
show databases;
use demo_app_development;
show tables;
select * from users;





Monday, December 30, 2013

Safari Extension - ZoomBySite - Set and remember zoom levels for individual web sites

This method initially worked OK but I found some problems with it.  For example, when composing a new email via gmail I could not see the "Send" button.  No good!

So I turned that off and am trying this extension instead: ZoomBySite

ZoomBySite is a Safari extension that:

1 - Lets you set a default zoom level for all sites
2 - Remembers a customized zoom level for specific sites


Sunday, December 29, 2013

Better fonts and colors in OSX Terminal

Open a Terminal session.

Terminal | Preferences | Settings | Homebrew
Font | Change
English | Menlo | Regular | 14
Default

Saturday, December 28, 2013

Part 2: How to install Rails and MySQL in Mavericks OSX

[Continued from Part 1: How to install Rails and MySQL in Mavericks OSX]

In Part 1, we installed Xcode, Homebrew, RVM, MySQL, and upgraded to the latest stable release of Ruby.

In Part 2, we install Git, Github, Gems, and Rails.

Install and configure Git

brew install git

I followed my Git notes here


Github


Generate SSH keys

While following the guide above, I set up a repository at Github called project1 to match the local repository I had previously initialized.  The only purpose of "project1" was to set up and test Github connectivity.

Set up GitHub repository

cd to the project1 subdirectory and run the following commands:

touch hello.txt
git init
git add .
git commit -m "first commit"
git remote add origin git@github.com:<yourusername>/project1.git
git push -u origin master


Now that we know we can push to Github, we can remove the test files.  Remove project1 from both the local host and Github.

Gems


rvm install ruby
rvm use ruby --default
rvm rubygems current

This bumped me up to Ruby 2.1.0 and installed rubygems (among other things).

Rails

And - FINALLY - install Rails:

gem install rails