Kang-Kyu Lee

Ruby + Rails codewriter

Page 3


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 →


setup Rails ubuntu 14.04 LTS

what I tried

ubuntu 14.04 LTS install / and update
—after that
open terminal ctl+alt T
install git - at ubuntu software center

(need git to install rbenv)

$ git --version
git version 1.9.1

install rbenv

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL 
$ type rbenv
"rbenv is a function"

https://github.com/sstephenson/rbenv#installation
https://github.com/sstephenson/ruby-build#readme

$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ rbenv install -list

install three of ruby versions

$ rbenv install 1.9.3-p547
$ rbenv install 2.0.0-p481
$ rbenv install 2.1.2

rbenv install 2.0.0-p451 didn’t work so tried

$ rbenv global 1.9.3-p545
$ ruby -v
ruby 1.9.3p545

and then tried again

$ rbenv install 2.0.0-p451
...

Continue reading →


install zsh ubuntu 14.04 LTS

What I did is…

open ubuntu software center and install “Shell with lots of features” (the first one on top when search by “zsh”)

then did this (alternate way to install zsh) and, got “don’t have to” message.

$ sudo apt-get update && sudo apt-get install zsh 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
zsh is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 21 not upgraded.

then went to “oh-my-zsh” and follow the guides

$ git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
$ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
$ chsh -s /bin/zsh 

reopen terminal and then… restart Ubuntu and then.

$ subl ~/.zshrc

open and edit this file…

ZSH_THEME="robbyrussell"
plugins=(git)

into

ZSH_THEME="ys"
plugins=(git rbenv gem bundler)

find themes

$ cd ~/.oh-my-zsh/themes/
$ ls

https...

Continue reading →