Kang-Kyu Lee

Ruby + Rails codewriter

Page 3


setup vagrant Ubuntu 12.04 LTS

when built-in vagrant and built-in virtualbox are outdated -
And we must use system ruby but its version is 1.8 and outdated -

upgrade ruby

sudo apt-get install ruby1.9.3
sudo apt-get autoremove
sudo apt-get remove ruby1.8

upgrade virtualbox & vagrant

sudo apt-get remove virtualbox
sudo apt-get remove vagrant

and do not install them from Ubuntu Software Center, download from website most recent version of them.

such as,

sudo dpkg -i vagrant_1.6.5_x86_64.deb
sudo dpkg -i virtualbox-4.3_4.3.20-96996-Ubuntu-precise_i386.debdpkg

and then, follow regular guides and vagrant up and vagrant provision as needed.

View →


local time in Rails app

This is what I found:

when I did Time.now in Ruby, it returns my local time (my system time, I should say).
when I did Time.zone.now or Time.now.in_time_zone in any Rails file it shows the time as set it up on config/application.rb:

config.time_zone = 'Central Time (US & Canada)'

..and such friendly names here you can list up when rake time:zones:all on command line.

http://www.ruby-doc.org/core-2.1.5/Time.html
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

Anyway, when a user access my app it doesn’t show their local time, but the time as set up on config/application.rb. And for local use (rails server) it’s enough to call Time.now for our current time, but this is quite meaningless because when you deploy on heroku, its system uses UTC time.

And fyi Rails saves it as UTC time in the database also… even though it returns local time as it has on config file.

You...

Continue reading →


form_for checkbox associated with label

I put a form with checkbox, submit button, and label in iteration block. And I wanted each label clickable.

<%= @tasks.each do |task| %>

  <%= form_for task, remote: true do |f| %>

    <%= f.check_box :complete %>
    <%= f.submit "update" %>
    <%= f.label :complete, task.name %>

  <% end %>

<% end %>

But clicking any label only ticked the first checkbox on the list.

First, for a clickable label in HTML, attribute name of label tag should match id name of input tag.

stackoverflow “How to create an HTML checkbox with a clickable label”

<label><input type="checkbox" name="checkbox" value="value">Text</label>

or should be

<input type="checkbox" name="checkbox" id="checkbox_id" value="value">
<label for="checkbox_id">Text</label>

Next, html.erb code above renders HTML with all the same id and for for each line.

<form accept-charset="UTF-8" action="/tasks/1"
...

Continue reading →


How do you install ruby before 1.9.3?

How do you install ruby before 1.9.3?

rbenv install --list
rbenv install 1.9.2-p180

old version error. Need apple-gcc42,

$ ls -l /usr/bin | grep gcc
-rwxr-xr-x   1 root   wheel     14224 Apr  8 14:45 gcc
lrwxr-xr-x   1 root   wheel         5 Apr  8 14:45 llvm-gcc -> clang
brew tap homebrew/dupes ; brew install apple-gcc42

as on the guide of error message

ls -l /usr/bin | grep gcc
-rwxr-xr-x   1 root   wheel     14224 Apr  8 14:45 gcc
lrwxr-xr-x   1 root   wheel         5 Apr  8 14:45 llvm-gcc -> clang

looks the same… where did it go, gcc42 ?

rbenv install 1.9.2-p180

is now working, however. Only dunno why..

View →


Sass sublime text

Sass highlighting seems not a default on Sublime text at the moment. I looked it up and found links

http://sublimetexttips.com/how-to-add-sass-support-in-sublime-text/
https://github.com/nathos/sass-textmate-bundle

install Sublime Package Control (if didn’t yet)

in Sublime Text running

  • command + shift + P and type
  • install package (“Package Control: Install Package”) then enter
  • Sass (“Sass”) then enter

now you have “Sass” on the list

  • View > Syntax > Open all with current extension as…

and pick “Sass”.

View →


ERB tag shortcut sublime text

https://github.com/eddorre/SublimeERB

go https://sublime.wbond.net/ and install Package Control

command + shift + P
install package (then enter)
SublimeERB (then enter)

(that goes to ~/.config/sublime-text-2/Packages/SublimeERB)

menu > preferences > Key Bindings - User
and then type in

  [
    { "keys": ["ctrl+shift+."], "command": "erb" }
  ]

and then when control + shift + .
erb tag happens

View →


using has_secure_password

For authentication, we need to do following three:

  • to model, has_secure_password
  • to table, password_digest
  • bundle bcrypt gem

So, we add has_secure_password to User model,

  has_secure_password

and it requires gem ‘bcrypt-ruby’ to the Gemfile,

  gem 'bcrypt-ruby'

$ bundle install

it makes some gibberish for us. and then we add another column (by convention, name) password_digest.

$ rails generate migration add_password_digest_to_users
  add_column :users, :password_digest, 'string'

$ rake db:migrate

When we set password attribute to user instance like,

> user = User.find 1
> user.password = "open_sesame"
> user.password_confirmation = "open_sesame"
> user.save

For information, actually has_secure_password has its validation in it – A password attribute value (and a password_confirmation value are) required to save the object. Or we can

  has_secure_password
...

Continue reading →


Put comments under a post

How-to from “just a post” to “a post with comments under it” page.

We will have:

  • comment creation form in the post show page
  • list of all comments after.

We need:

  • nested resources (need routing supports that generated url)
  • model-backed form of comment object
  • action in controller (create action)
  • rendering validation errors of it.

put a (resourceful) routing for comments

resources :posts do
  resources :comments, only: :create
end

and it has now URL

<form action="/posts/:id/comments" method="post">

and params from the form

params 
=>  { ...
 "comment"=>{"body"=>"this is a comment"},
 "commit"=>"Create Comment",
 "controller"=>"comments",
 "action"=>"create",
 "post_id"=>"6"}

so now we change posts_controller show action

@comment = Comment.new

and comments_controller create action like,

@comment = Comment.new(params.require(:comment).permit(:body))
@post =
...

Continue reading →


Rails prototype Post-It: Lesson 1

This is what I have done on week 1, course 2 of Tealeaf Academy:

Database tables - schema view

  • posts: ‘url’, ‘title’, ‘description’
  • users: ‘username’
  • comments: ‘body’
  • categories: ‘name’
  • foreign keys (and primary keys)

Migration files

$ rails generate migration create_posts
$ rails g migration create_users
$ rails g migration add_user_id_to_posts
$ rails g migration create_comments
$ rails g migration create_categories
$ rails g migration create_post_categories

posts

create_table
  t.string :url, :title
  t.text :description

users

  t.string :username

add user_id to posts

add_column :posts, :user_id, 'integer'

comments

create_table
  t.text :body
  t.integer :post_id, :user_id

category

  t.string :name

join table for many to many association of post and category

  t.integer :post_id, :category_id

for all do not forget

  t.timestamps

build schema file and

...

Continue reading →


split the window

How you view sublime text split window. I looked up everywhere finally found it.. before to forget

⌘ + ⌥ + 2 Split view into two columns

Other Shortcuts here - Keyboard Shortcuts for Mac OS X

View →