kaku'blog

我是一个在黑暗中大雪纷飞的人啊


  • 首页

  • 归档

  • 标签

LPIC学习笔记

发表于 2015-12-01   |   分类于 linux   |  

今日のLPIC勉強メモ

1.自分の所属グループを表示するにはgroupsコマンドを使う。

2.ファイル作成時にアプリケーションが指定するパーミッションが666

3.ディレクトリ作成時指定するパーミッションが777

4.umaskコマンドでの変更は子プロセスでのみ有効に成る

5.SUIDはパーミションに4000もしくは所有者にsを付与する  SUIDの使用例としては/usr/bin/passwdがある   
passwdコマンドで/etc/shadowファイルの書き換えを実行時どのユーザが実行した場合でもroot権限で実行されるようになっています。

6.スティッキービットが特定のディレクトリに対してアクセス権が許可されていてもファイルの削除はおこえないよう保護する設定です。  
 chmod 1000 ファイルもしくはchmod o+t   

vim cheat sheet

发表于 2015-11-27   |  

发现了一张特别牛逼的图。
用了vim以后会上瘾的。

vim cheat sheet

如何重新搭建一个已经存在的Octopress博客

发表于 2015-11-27   |   分类于 blog   |  
1
2
3
4
5
6
7
8
9
10
$ git clone git@github.com:username/username.github.com.git
$ cd username.github.com
username.github.com$ git checkout source
username.github.com$ mkdir _deploy
username.github.com$ cd _deploy
username.github.com/_deploy$ git init
username.github.com/_deploy$ git remote add origin git@github.com:username/username.github.com.git
username.github.com/_deploy$ git pull origin master
username.github.com/_deploy$ cd ..
username.github.com$

rails gem系列之CarrierWave

发表于 2015-02-11   |   分类于 rails gem ruby   |  

##概要
CarrierWave是rails中用来上传文件的gem。类似的gem还有PaperClip。

##和PaperClip的区别
1.PaperClip简单易用

2.CarrierWave的功能较多,应用广泛

##用法

先来做一个测试用的project吧

1
2
3
4
5
rails new test
cd test
rails g scaffold Product name:string
rake db:migrate
rails s

加入CarrierWave的gem

Gemfile
1
gem 'carrierwave'

安装

1
bundle install

生成carrierwave的配置文件

1
2
rails g uploader Image
create app/uploaders/image_uploader.rb

加入image列

1
2
rails g migration add_image_to_product image:string
rake db:migrate

向model中加入mount_uploader方法

1
2
3
4
# app/models/product.rb
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
end

向form中加入上传组件,其中指定hidden属性image_cache的作用就是当提交form时发生error上传的文件或者图片可以保存起来,不用再上传一次。

_form.html.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- 开始 -->
<div class="field">
<% if @product.image? %>
<div class="thumbnail">
<%= image_tag @product.image.url %>
</div>
<% end %><br>
<%= f.label :image %><br>
<%= f.file_field :image %>
<%= f.hidden_field :image_cache %>
</div>
<div class="field">
<!-- 既存product且存在图片-->
<% if @product.persisted? && @product.image? %>
<label>
<%= f.check_box :remove_image %>
删除
</label>
<% end %>
</div>
<!-- 结束 -->

别忘了要在controller中加一下StrongParameter

products_controller.rb
1
2
3
def product_params
params.require(:product).permit(:name, :price, :image, :image_cache, :remove_image)
end

添加显示部分的image_tag代码

show.html.erb
1
2
3
4
5
6
7
8
<p>
<strong>Image:</strong>
<% if @product.image? %>
<div class="thumbnail">
<%= image_tag @product.image.url %>
</div>
<% end %>
</p>

就这些,大功告成,很简单。

rails gem系列之ransack

发表于 2015-02-08   |   分类于 rails gem   |  

##概要

ransack是一个用来实现检索功能的gem。

github地址: https://github.com/activerecord-hackery/ransack

示例网站:http://ransack-demo.herokuapp.com

##安装

Gemfile
1
gem 'ransack'

然后执行bundle就可以了。

##使用方法

在controller中如下使用,注意参数:q为ransack的固定写法。里面放了view传过来的检索项的值。

1
2
3
4
def index
@q = Person.ransack(params[:q])
@people = @q.result(distinct: true)
end

view中的写法如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%= search_form_for @q do |f| %>

# Search if the name field contains...
<%= f.label :name_cont %>
<%= f.search_field :name_cont %>

# Search if an associated articles.title starts with...
<%= f.label :articles_title_start %>
<%= f.search_field :articles_title_start %>

# Attributes may be chained. Search multiple attributes for one value...
<%= f.label :name_or_description_or_email_or_articles_title_cont %>
<%= f.search_field :name_or_description_or_email_or_articles_title_cont %>

<%= f.submit %>
<% end %>

##search的写法

eq表示等于

1
2
Item.search(:name_eq => 'test').result.to_sql
# => "SELECT `items`.* FROM `items` WHERE `items`.`name` = 'test')"

lt表示小于

1
2
Item.search(:price_lt => 1000).result.to_sql
# => "SELECT `items`.* FROM `items` WHERE `items`.`price` < 1000)"

gt表示大于

1
2
Item.search(:price_gt => 1000).result.to_sql
# => "SELECT `items`.* FROM `items` WHERE `items`.`price` > 1000)"

in表示sql中的in

1
2
Item.search(:category_id_in => [5,10,15,20]).result.to_sql
# => "SELECT `items`.* FROM `items` WHERE `items`.`category_id` IN (5,10,15,20))"

cont表示包含,模糊查询

1
2
Item.search(:name_cont => 'test').result.to_sql
# => "SELECT `items`.* FROM `items` WHERE `items`.`name` LIKE '%test%')"

start表示前端一致

1
2
Item.search(:name_start => 'test').result.to_sql
# => "SELECT `items`.* FROM `items` WHERE `items`.`name` LIKE 'test%')"

组合用法

1
2
3
4
Item.search(:name_and_description_cont => 'test').result.to_sql
# => "SELECT `items`.* FROM `items` WHERE (((`items`.`name` LIKE '%test%') AND (`items`.`description` LIKE '%test%')))"
Item.search(:name_or_description_cont => 'test').result.to_sql
# => "SELECT `items`.* FROM `items` WHERE (((`items`.`name` LIKE '%test%') OR (`items`.`description` LIKE '%test%')))"

条件组合
末尾加_all是AND关系,末尾加_any是OR关系

1
2
3
4
Item.search(:name_cont_all => ['AAA', 'BBB']).result.to_sql
# => "SELECT `items`.* FROM `items` WHERE (((`items`.`name` LIKE '%AAA%') AND (`items`.`name` LIKE '%BBB%')))"
Item.search(:name_cont_any => ['AAA', 'BBB']).result.to_sql
# => "SELECT `items`.* FROM `items` WHERE (((`items`.`name` LIKE '%AAA%') OR (`items`.`name` LIKE '%BBB%')))"

相关模型检索

1
2
Item.search(:comments_body_cont => 'test').result.to_sql
# => "SELECT `items`.* FROM `items` LEFT OUTER JOIN `item_comments` ON `item_comments`.`item_id` = `items`.`id` WHERE `item_comments`.`body` LIKE '%test%'"

##表头排序

1
<%= sort_link(@q, :name) %>

加别名,加默认排序

1
<%= sort_link(@q, :name, 'Last Name', default_order: :desc) %>

rails gem系列之kaminari

发表于 2015-02-07   |   分类于 rails gem   |  

##概要
kaminari是rails项目中常用的分页gem

github地址:https://github.com/amatsuda/kaminari

##安装

1
gem 'kaminari'

然后执行bundle就可以了。

##基本使用

分页的写法如下,默认为25行一页。改行数在后面加.per(行数)就可以了。

1
2
3
def index
@items = Item.page(params[:page])
end

view中加一行就会生成翻页部分了。so easy.

index.html.erb
1
<%= paginate @items %>

##更改翻页样式

1
rails g kaminari:views default

这样就会在app/views/kaminari下生成翻页部分的代码,可以按自己的喜好修改。

当然我们还可以选择bootstrap样式的翻页。直接clone下来就可以用了。当然前提是要先装好bootstrap。
https://github.com/gabetax/twitter-bootstrap-kaminari-views

##ajax翻页

posts_controller.rb
1
2
3
4
5
6
respond_to :html, :js

def index
@posts = Post.page(params[:page]).per(10)
respond_with(@posts)
end

列表部分放到partial中

_posts.html.erb
1
2
<% @posts.each do |post| %>
省略

index.js.erb
1
$("#posts").html("<%= j(render :partial => 'posts') %>");
index.html.erb
1
2
3
4
5
 <div id="posts">
<%= render :partial => "posts" %>
</div>

<%= paginate @posts %>

rails gem系列之ffaker

发表于 2015-02-06   |   分类于 rails gem   |  

##概要

ffaker可以用来生成测试数据,在rails的seed.rb文件中写少量的代码然后执行
rake db:seed 就可以生成大量的测试数据。

fmaker的github地址:
https://github.com/emmanueloga/ffaker

##和faker的关系

  1. ffaker是faker的重写版。
  2. ffaker要比faker执行快。

##和faker的速度比较

1
2
3
4
5
6
def bench1000
start = Time.now
1000.times{yield}
finish = Time.now
puts "Time: #{(finish - start).to_f}"
end

faker的执行时间

1
2
3
require 'faker'
bench1000{Faker::Name.name}
# => Time: 0.186539

ffaker的执行时间

1
2
3
require 'ffaker'
bench1000{Faker::Name.name}
# => Time: 0.009986

快了不止一点点啊。。。。

##使用方法

安装

1
gem install ffaker

在seed.rb中添加类似这样的生成代码,然后执行rake db:seed就可以生成测试数据了。

1
2
3
10.times do
User.create(name: Faker::Name.name, profile: Faker::Lorem.sentence(10))
end

##常用API

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Faker::Name.name      #=> "Christophe Bartell"

Faker::Internet.email #=> "kirsten.greenholt@corkeryfisher.info"

Faker::PhoneNumber.cell_phone #=> "(186)285-7925"

Faker::Lorem.sentence #=> "Dolore illum animi et neque accusantium."

Faker::Lorem.paragraph #=> "Neque dicta enim quasi. Qui corrupti est quisquam. Facere animi quod aut. Qui nulla consequuntur consectetur sapiente."

Faker::Number.number(10) #=> "1968353479"

Faker::Internet.password(8) #=> "yfgjik0hgzdqs0"

Faker::Business.credit_card_number #=> "1228-1221-1221-1431"

其他详细用法请详见这里 http://www.rubydoc.info/gems/faker/1.4.3

rails gem系列之settingslogic

发表于 2015-02-04   |   分类于 rails gem   |  

##概要

可以从配置文件简单读取配置的gem

github地址:https://github.com/binarylogic/settingslogic

安装

1
gem install settingslogic

定义class app/models/settings.rb

1
2
3
4
class Settings < Settingslogic
source "#{Rails.root}/config/application.yml"
namespace Rails.env
end

创建配置文件 config/application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
defaults: &defaults
cool:
saweet: nested settings
neat_setting: 24
awesome_setting: <%= "Did you know 5 + 5 = #{5 + 5}?" %>

development:
<<: *defaults
neat_setting: 800

test:
<<: *defaults

production:
<<: *defaults

之后在rails console里面测试一下,就可以拿到配置文件里的值了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>> Rails.env
=> "development"

>> Settings.cool
=> "#<Settingslogic::Settings ... >"

>> Settings.cool.saweet
=> "nested settings"

>> Settings.neat_setting
=> 800

>> Settings.awesome_setting
=> "Did you know 5 + 5 = 10?"

安装auto-fu.zsh让命令输入自动补全

发表于 2015-02-01   |  

  • 下载auto-fu.zsh
1
2
cd ~/.oh-my-zsh/custom/plugins
git clone https://github.com/hchbaw/auto-fu.zsh.git auto-fu
  • 执行zcompile
1
A=~/.oh-my-zsh/custom/plugins/auto-fu/auto-fu.zsh; (zsh -c "source $A ; auto-fu-zcompile $A ~/.zsh")
  • 添加下面的设置到zshrc中
1
2
3
4
5
6
7
## auto-fu.zsh stuff.
# source ~/.oh-my-zsh/custom/plugins/auto-fu/auto-fu.zsh
{ . ~/.zsh/auto-fu; auto-fu-install; }
zstyle ':auto-fu:highlight' input bold
zstyle ':auto-fu:highlight' completion fg=black,bold
zstyle ':auto-fu:var' postdisplay $'\n-azfu-'
zle-line-init () {auto-fu-init;}; zle -N zle-line-init
  • 重新编译zshrc
1
source ~/.zshrc

zsh下git命令Cheat-sheet

发表于 2015-02-01   |   分类于 git zsh   |  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
g - git
gst - git status
gl - git pull
gup - git pull --rebase
gp - git push
gd - git diff
gdc - git diff --cached
gdv - git diff -w "$@" | view -
gc - git commit -v
gc! - git commit -v --amend
gca - git commit -v -a
gca! - git commit -v -a --amend
gcmsg - git commit -m
gco - git checkout
gcm - git checkout master
gr - git remote
grv - git remote -v
grmv - git remote rename
grrm - git remote remove
gsetr - git remote set-url
grup - git remote update
grbi - git rebase -i
grbc - git rebase --continue
grba - git rebase --abort
gb - git branch
gba - git branch -a
gcount - git shortlog -sn
gcl - git config --list
gcp - git cherry-pick
glg - git log --stat --max-count=10
glgg - git log --graph --max-count=10
glgga - git log --graph --decorate --all
glo - git log --oneline --decorate --color
glog - git log --oneline --decorate --color --graph
gss - git status -s
ga - git add
gm - git merge
grh - git reset HEAD
grhh - git reset HEAD --hard
gclean - git reset --hard && git clean -dfx
gwc - git whatchanged -p --abbrev-commit --pretty=medium
gsts - git stash show --text
gsta - git stash
gstp - git stash pop
gstd - git stash drop
ggpull - git pull origin $(current_branch)
ggpur - git pull --rebase origin $(current_branch)
ggpush - git push origin $(current_branch)
ggpnp - git pull origin $(current_branch) && git push origin $(current_branch)
glp - _git_log_prettily
12
kaku

kaku

17 日志
9 分类
RSS
github twitter weibo douban zhihu
© 2016 kaku
由 Hexo 强力驱动
主题 - NexT.Muse