2020年5月22日 星期五

rails todo app 製作

```
$ rails g scaffold Case casename:string owner:string note:text duetime:time active:boolean
```

2020年5月12日 星期二

Rails登入功能

generate scaffold member

創造member的scaffold

$ rails g scaffold member name email password_digest

修改create tbale的一些東西,增加了session_token
db/migrate/20xxxxx_create_members.rb
    def create  
        t.string :name, unique: true,
        t.string :email, unique: true,
        t.string :password, 
        + t.string :session_token, unique: ture, index,true
        t.timestamps
    end
end



2020年5月11日 星期一

Rails 在console中將所有沒有session_token的都掃檢查一遍

將所有沒有session_token的儲存條目都重新掃檢查一遍

$ Rails c 
$ User.all.each {|user| user.save(validate: false)}
上面這條跟
user.all.each do |user|
    user.save(validate: false)
end
一樣的意思,只是寫成一整行。

Rails 新增session_token model的欄位

https://ihower.tw/rails/migrations.html

用migration新增model的欄位(儲存格)

 $ Rails g migration add_session_token_to_user session_token:string 

增加欄位

ctrlp>db/migrate/20xxxxxxx_add_session_token_to_user.rb
def change
    add_column :users, :session_token, :string
    add_index :users, :session_token, unique: true
    或是以上兩行換成add_column :users, :session_token, :string  ,:index => { :unique => true }
end

變更資料庫

$ rails db:migrate
$ rails c
>User.first >>看看有沒有新增加成功

更新model的檔案

ctrlp>app/models/user.rb
class User < ActiveRecord::Base
    #||=代表<或者等於>
    #這行表示,在儲存之前,假如session_token沒有資料,那就要自己創密碼的token一個存入。
    before_save {self.session_token ||= Digest::SHA1.hexdigest(SecureRandom.urlsafe_base64.to_s)}
end
         

Rails 的session觀看

在controller裡面塞進session

ctrlp>
def sandbox
    session[:our_data] = 1234
end

在瀏覽器view裡面看session

在sandbox.html.erb裡面看
ctrlp>app/views/root/sandbox.html.erb

  • <%=session.to_h%>
  • <%=session.inspect%>
重點在於session_id跟csrf__token跟our_data。


2020年5月10日 星期日

Rails的form_for new.html.erb通用檔案

ctrlp>app/views/users/new.html.erb
<h1>Sign Up</h1>
<div class="row">
    <%=form_for @user do |f|%>
        <%=f.label :name%>
        <%=f.text_field :name%>

        <%=f.label :email%>
        <%=f.text_field :email%>

        <%=f.label :password%>
        <%=f.text_field :password%>

        <%=f.label :password_confirmation%>
        <%=f.text_field :password_confirmation%>

        <div>
            <%=f.submit "Register!", class: "btn btn-primary"%>
        </div>
    <%end%>
</div>

Rails在console中增加刪除查詢改資料

user = User.create(name: "Lisa", email: "lisa@email.com", password: "passwrod", password_confirmation: "password")
user = User.first
user= User.last
user = User.find_by(id: "1")
user = User.find_by(name: "Lisa")
user = User.find_by(email: "lisa@email.com")
User.find(1).destroy


user = Candidate.new(name: "孫悟空", age: 18)
user.save

刪除全部

@object = Object.all
@object.each do |o|
  o.delete
end

Rails helper gravatar_for

ctrlp>app/helpers/users_helper.rb
module UsersHelper
    def gravatar_for(user)
        gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
        gravatar_url = "https://secure.gravatar.com/avatar/#(gravatar_id)
        image_tag(gravatar_url, alt: user.name, class: "gravatar")
    end
end

2020年5月7日 星期四

rails 密碼加密儲存gem bcyrpt-ruby

http://disco26.logdown.com/posts/175700-30-coder100-day-257-rails-digest-12-has-secure-password
https://andyyou.github.io/2015/04/04/login/
1.安裝bcrypt-ruby的gem
ctrlp>Gemfile

  • gem 'bcrypt-ruby'
$ bundle install
關閉server
$ rails s #重開server
3.增加password欄位
$ rails g migration add_passwrod_digest_to_member #幫member的model增加欄位PasswordDigest

db/migrate/.....XXX_add_password_digest_to_member.rb
def change
    add_column :users, :password_digest, :string #增加欄位
end

4.增加helper method
ctrlp>app/model/user.rb
class User < ActiveRecord::Base
    has_secure_password #這是一個helper method
    has_secure_password validations: false #將兩次輸入的方法去除
end

5. $ rails db:migrate
6.在Rails confole添加資料
user = Member.find_by(id: 1)
user.password  = user.password_confirmation = "secret"
user.save
user>>會秀出user的資料
輸入錯誤的密碼
> user.authenticate("wrong_password")
=>false 認證失敗

輸入正確的密碼
>user.authenticate("secret")
=>秀出正確的user資料
認證成功

2020年5月5日 星期二

vim基礎設定

vim模式中Esc的替代
三種方法
  1. Esc    >>但是太遠
  2. ctrl+] >>有點遠
  3. ctrl+c >>之後都用這個

:h key-notation 看特別的鍵在vim裡面是什麼名字
:map 看現在的mapping

2020年5月2日 星期六

laravel routes設定路由

  • Route::resource('articles', 'ArticlesController'); 

可以用get或是post
Route::get('foo', function () {
    return 'Hello World';
});

Route::get('foo', function () {
    return view('foo');
});

Route::get('foo', function () {
    return view('foo');
});

Route::post('foo', function () {
    //
});

登入myphp


  • mysql -u root -p >
homestead的mysql密碼是secret

linux .bash_profile設定

http://linux.vbird.org/linux_basic/0320bash.php

  • source .bash_profile   >更新剛剛配置的環境變量

2020年5月1日 星期五

virtualbox問題


  • A VirtualBox machine with the name 'homestead' already exists.Please use another name or delete the machine with the existing name, and try again.


如何刪除virtual machine

  • vboxmanage list vms

linux下面的shell script

在使用vagrant的時候,有使用到一條指令: sh init.sh
指令跑玩後就多出一堆東西。覺得很厲害。

原來還有一條shell script可以代替手動輸入一大堆的資料。真的是很方便呢。
http://linux.vbird.org/linux_basic/0340bashshell-scripts.php
參考鳥哥的私房菜

這個shell script超牛的。

cloud9上面開發rails

 使用IAM身分登入,以免有資安問題。(用admin帳號登入,創建iam使用者,然後在上面的連結登入) 選擇 ubuntu18.04 減少查詢成本。 已經是超級使用者了,sudo 什麼都不需要輸入密碼了。 $ ruby -v  檢查 ruby使用的版本>>2.6.3,...