Tuesday, May 13, 2014

rails paperclip disable validation

1, Disable from global config file: config/application.rb

require 'paperclip/media_type_spoof_detector'
module Paperclip
  class MediaTypeSpoofDetector
    def spoofed?
      false
    end
  end
end

2, Disable from controller
class Logserver < ActiveRecord::Base
  has_attached_file :serverkey, :path => "public/keys/:id/:filename", :url => "/keys/:id/:basename.:extension"
  #validates_attachment_content_type :serverkey, :content_type => /\Atext.*\Z/
  do_not_validate_attachment_file_type :serverkey
 end

Wednesday, April 30, 2014

install awscli

apt-get install python-pip
pip install awscli

aws configure

aws ec2 describe-instances

aws ec2 run-instances --image-id ami-3495735c --count 1 --instance-type t1.micro

Friday, February 28, 2014

Github/Markdown Gem usage

Under root:
1, install github markdown gem
gem install github-markdown

2, Add markdown gem into Gem file
 root@ubuntuChef:/opt/ruby/user# vim Gemfile
gem 'github-markdown'

3, may need do:
bundle install

4, Define your markdown in your controller
I put MD file under app/assets/markdown folder

ang@ubuntuChef:/opt/ruby/user$ vim app/controllers/myusers_controller.rb

  def markdown
    #@myusers = Myuser.all
    require 'github/markdown'
    #require 'rdiscount'

    mdfile = File.join(Rails.root, 'app', 'assets', 'markdown', 'test.md')
    mymd = File.open(mdfile, 'rb') { |file| file.read }
    @md = GitHub::Markdown.render(mymd)
    #@md =  RDiscount.new(mymd, :smart, :filter_html).to_html
  end

5, update the view  - DON'T forget RAW

root@ubuntuChef:/opt/ruby/user# vim app/views/myusers/markdown.html.erb
<h1>Markdown</h1>

<%= raw(@md) %>

Github tutorial

1,  generate ssh key and add your public key to github
https://help.github.com/articles/generating-ssh-keys

2, test connection with github
$ ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '192.0.52.28' to the list of known hosts.
Hi gds! You've successfully authenticated, but GitHub does not provide shell access.

3, Add your project to Github
A, create a repo in github

B, in your local box,
DO:
$ git config --global user.name "Smith Chris" $ git config --global user.email schris@example.com

touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/gdsdong/csstemplate-acupuncture.git
git push -u origin master
 
git remote add origin git@github.com:gdsdong/csstemplate-decoration.git
git push -u origin master 
 
Error:
$ git push -u origin master
error: The requested URL returned error: 403 while accessing 
https://github.com/gdsdong/csstemplate-acupuncture.git/info/refs
Need to update local git config
  1. edit .git/config file under your repo directory
  2. find url=entry under section [remote "origin"]
  3. change it from url=https://github.com/xxxx/lxxx.git  to url=ssh://git@github.com/xxxx/lxxx.git. that is.
  4. Save config file and quit. now you could use git push origin master to sync your repo on GitHub




Thursday, February 27, 2014

Ruby on Rails Paperclip tutorial - rails 4.0.3, Ruby 2.1, Paperclip 4.1.1

Step 1, New a project
rails new demo

Step 2, Update Gemfile
add paperclip, mysql2 gem, enable JavaScript runtime
gem 'mysql2'
gem 'paperclip'
gem 'therubyracer', platforms: :ruby // remove the comment


You should be able to run rails server now:
ang@ubuntuChef:/opt/ruby/demo$ rails server
=> Booting WEBrick
=> Rails 4.0.3 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2014-02-27 18:39:15] INFO  WEBrick 1.3.1
[2014-02-27 18:39:15] INFO  ruby 2.1.1 (2014-02-24) [x86_64-linux]
[2014-02-27 18:39:15] INFO  WEBrick::HTTPServer#start: pid=3187 port=3000

Step 3, Creating the Database

ang@ubuntuChef:/opt/ruby/demo$ rails generate scaffold User username:string

Your migration file looks like:
ang@ubuntuChef:/opt/ruby/demo$ less db/migrate/20140227235934_create_users.rb

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :username

      t.timestamps
    end
  end
end

add paperclip  - one is image, one is zip file


ang@ubuntuChef:/opt/ruby/demo$ rails generate paperclip User avator download

ang@ubuntuChef:/opt/ruby/demo$ vim db/migrate/20140228000158_add_attachment_avator_download_to_users.rb

class AddAttachmentAvatorDownloadToUsers < ActiveRecord::Migration
  def self.up
    change_table :users do |t|
      t.attachment :avator
      t.attachment :download
    end
  end

  def self.down
    drop_attached_file :users, :avator
    drop_attached_file :users, :download
  end
end
 
Step 4, Applying the Migration - there are two migrations.

ang@ubuntuChef:/opt/ruby/demo$ rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0196s
==  CreateUsers: migrated (0.0199s) ===========================================

==  AddAttachmentAvatorDownloadToUsers: migrating =============================
-- change_table(:users)
   -> 0.2719s
==  AddAttachmentAvatorDownloadToUsers: migrated (0.2721s) ====================

Step 5, Start server
http://192.168.88.216:3000/users/new

You will see the new user page, but no file upload options. We need to add them manually.

Step 6, update model
ang@ubuntuChef:/opt/ruby/demo$ vim app/models/user.rb

class User < ActiveRecord::Base
  has_attached_file :avator
  has_attached_file :download
  validates_attachment_content_type :avator, :content_type => %w(image/jpeg image/jpg image/png application/pdf)
  validates_attachment_content_type :download, :content_type => %w(application/octet-stream  application/x-zip application/x-zip-compressed application/pdf application/x-pdf)
end



Step 7, update view
ang@ubuntuChef:/opt/ruby/demo$ vim app/views/users/_form.html.erb



  <div class="field">
    <%= f.label :avator %><br>
    <%= f.file_field :avator %>
  </div>
  <div class="field">
    <%= f.label :download %><br>
    <%= f.file_field :download %>
  </div>







ang@ubuntuChef:/opt/ruby/demo$ vim app/views/users/show.html.erb

<p>
  <strong>Avator:</strong>
 <%= image_tag @user.avator.url %>
<%= image_tag @user.avator.url(:thumb) %>
</p>
<p>
  <strong>Download:</strong>
  <a href="<%= @user.download.url %>" > Download File </a>
</p>




Step 8, update controller
ang@ubuntuChef:/opt/ruby/demo$ vim app/controllers/users_controller.rb

    def user_params
      params.require(:user).permit(:username, :avator , :download)
    end





Step 9
Run server:
ang@ubuntuChef:/opt/ruby/demo$ rails server




Monday, February 24, 2014

ipad php session issue / not working

I have user login script, user session destroyed when redirect the page.

Here is the code:

session_start() ;

if ( $_SESSION["logged_in"] != 1 )
{
    header("Location: index.php");
}

Need to add exit to fix session destroyed issue. 
session_start() ;

if ( $_SESSION["logged_in"] != 1 )
{
    header("Location: index.php");
 
    exit;
}