2015年3月27日 星期五

migrate database from sqlite3 to postgresql on development of rails 4

http://stackoverflow.com/questions/6710654/how-do-you-easily-change-from-sqlite-to-postgresql-in-rails

http://railscasts.com/episodes/342-migrating-to-postgresql

http://www.gotealeaf.com/blog/how-to-install-postgresql-on-a-mac


download postgresql  from postgresql.org

1
initdb /usr/local/var/postgres
install  postgresql


$ createuser f3 -d -s
$ createdb -Of3 -Eutf8 f3_development
$ createdb -Of3 -Eutf8 f3_test
Update the Gemfile
gem 'sqlite3'
gem 'pg'
gem 'taps'
$ bundle
Update database.yml
#development:
#  adapter: sqlite3
#  database: db/development.sqlite3
#  pool: 5
#  timeout: 5000

development:
  adapter: postgresql
  encoding: unicode
  database: f3_development
  pool: 5
  username: f3
  password:

#test:
#  adapter: sqlite3
#  database: db/test.sqlite3
#  pool: 5
#  timeout: 5000

test:
  adapter: postgresql
  encoding: unicode
  database: f3_test
  pool: 5
  username: f3
  password:
Start the taps server on the sqlite database
$ taps server sqlite://db/development.sqlite3 user password
Migrate the data
$ taps pull postgres://f3@localhost/f3_development http://user:password@localhost:5000
Restart the Rails webserver
$ rails s
Cleanup the Gemfile
#gem 'sqlite3'
gem 'pg'
#gem 'taps'
$ bundle

2015年3月22日 星期日

friendly_id + 中文顯示

http://waynechu.logdown.com/posts/205700-rails-web-site-no-longer-displays-only-id

rails generate friendly_id
rails generate migration add_slug_to_products slug:string:uniq
rake db:migrate
class Product < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged
end
接著到config/initializers/friendly_id.rb下把自動查找的功能打開,將config.use :finders這段解除註解就可以了。
(不打開finders也可以,但就必須要把Product.find(params[:id])的地方改寫成Product.friendly.find(params[:id])
class Product < ActiveRecord::Base
 
  # 原本是input.to_s.parameterize,但是parameterize只支援英文跟數字,所以改用babosa的to_slug
  def normalize_friendly_id(input)
    input.to_s.to_slug.normalize.to_s
  end

2015年3月19日 星期四

Adding Custom Fields to Devise User Model in Rails 4

http://www.jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model-in-rails-4.html

 create the migration
class AddFieldsToUsers < ActiveRecord::Migration
  def change
    add_column :users:first_name:string
    add_column :users:last_name:string
  end
end

Customizing the RegistrationsController


creating our own registrations controller (which extends Devise) and then customizing these methods as we choose.

class RegistrationsController < Devise::RegistrationsController
 
  private
 
  def sign_up_params
    params.require(:user).permit(:first_name:last_name:email:password,:password_confirmation)
  end
 
  def account_update_params
    params.require(:user).permit(:first_name:last_name:email:password,:password_confirmation:current_password)
  end
end

Now we need to tell Devise to use our registrations controller - we can do this in ourconfig/routes.rb file.
devise_for :users:controllers => { registrations: 'registrations' }

2015年2月5日 星期四

first upload to github and deploy to heroku

1.Github上新增一個 repository


git init

Initialized empty Git repository in .git/

$ git add .

$ git commit -m "my first commit"
 
heroku create

heroku git:remote -a APP_NAME

git push heroku master

2014年12月9日 星期二

problems on Rals - brew reinstall opensslRuby Bundle Symbol not found: _SSLv2_client_method (LoadError)


http://stackoverflow.com/questions/25492787/ruby-bundle-symbol-not-found-sslv2-client-method-loaderror
http://stackoverflow.com/questions/18521304/bundle-install-fails-error-loading-rubygems-plugin-after-i-deleted-files
http://stackoverflow.com/questions/19262312/installing-pg-gem-failure-to-build-native-extension/19620569#19620569

rvm get stable
rvm reinstall ruby-1.9.3-p545 (got the latest version)

brew reinstall openssl

  • Install another build of Xcode Tools (typing 'brew update' in the terminal will prompt you to update the Xcode build tools)
  • brew update
  • brew install postgresql

2014年11月26日 星期三

godaddy + heroku

http://lifesforlearning.com/heroku-with-godaddy/

heoroku domains:add www.example.com
heroku domains:add example.com

log in to godaddy

  1. Then remove all records here except for the NameServers
  2. Add / edit the CNAME record for “www” to point to your Heroku app domain name like “example.herokuapp.com”. Mine is madpad.herokuapp.com.

2014年11月22日 星期六

elasticsearch + searchkick


https://intercityup.com/blog/installing-elasticsearch-mac-os-x-10-9-mavericks-development/


point $JAVA_HOME to the Java installation path in your .bash_profile:
export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"

brew install elasticsearch


And start elasticsearch with:
elasticsearch --config=/usr/local/opt/elasticsearch/config/elasticsearch.yml




https://shellycloud.com/blog/2013/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch

# Gemfile
gem "searchkick"
After running bundle install, we can configure the Book model to be searchable:
# app/models/book.rb
class Book < ActiveRecord::Base
  searchkick
  # ...
end
Next, we need to build the index for the Book class so that the books we already have in the database will be added to Elasticsearch. When new records are created rebuilding isn't necessary, but you have to do it whenever you change the searchkick method in your model:
rake searchkick:reindex CLASS=Book

https://github.com/ankane/searchkick

Deployment

Searchkick uses ENV["ELASTICSEARCH_URL"] for the Elasticsearch server. This defaults tohttp://localhost:9200.

Heroku

Choose an add-on: SearchBoxBonsai, or Found.
# SearchBox
heroku addons:add searchbox:starter
heroku config:add ELASTICSEARCH_URL=`heroku config:get SEARCHBOX_URL`

# Bonsai
heroku addons:add bonsai
heroku config:add ELASTICSEARCH_URL=`heroku config:get BONSAI_URL`

# Found
heroku addons:add foundelasticsearch
heroku config:add ELASTICSEARCH_URL=`heroku config:get FOUNDELASTICSEARCH_URL`
Then deploy and reindex:
heroku run rake searchkick:reindex CLASS=MODEL