Learn Rails5.2 Bundler ; Forms

如果一个Rubyer想要提供一个功能或某个程序或程序的集合给其他Rubyer使用,这个Rubyer可以创建一个package,这个package就叫做gems。

可以通过gem install安装。

 https://www.ruby-toolbox.com/

 

Raisl 本身就是一个gem。 

gem 'listen', '>= 3.0.5', '< 3.2' 这是说大于3.0.5版本并小于3.2版本。

'~> 2.0.0' 这是说最高版本用2.0.0

 

Gemfile.lock被用于多开发者共享相同的版本。

⚠️更新Gemfile,但不要改变Gefile.lock 

因为这个机制,你也可以平行使用不同的gem版本在多个Rails程序上。

 


 

更新版本bundle update

 

如果gemfile中,gem 'rails', '4.2.0',你需要改变到4.24。 

首先在gemfile中,改成gem 'rails', ''4.2.4', 然后再在terminal中输入bundle update rails

 

⚠️在每次gem更新后,你需要运行系统测试,来确保一个新的gem版本不会对程序起到负面影响 (完善的系统测试和单元测试很重要。)

 

bundle outdated (检验过时的gem)

命令是在更新完Rails版本后使用的,检验其他gem是否支持新版本。 如果有过时的gem,会在terminal上显示:

Outdated gems included in the bundle:
  * archive-zip (newest 0.10.0, installed 0.7.0)
  * websocket-driver (newest 0.7.0, installed 0.6.5
需要更新它们。

 


 

bundle exec 

 

bundle exec rake db:migrate

使用gemfile文件指定的gem版本。

如果你用gem install rake安装了10.1.0版本的rake(假设是最新的),当你直接使用调用rake时,使用的会是这个最新版本的rake。

如果项目的Gemfile中指定的版本是0.9.6(或者是Gemfile.lock中是0.9.6)的话,你如果不加bundle exec,将会用rake 10.1.0的版本去执行本来应该由0.9.6版本的rake写出的Rake task。

会不会出问题?可能会,可能不会。因为很有可能原作者使用0.9.6版本的rake写的Rake task中没有什么被废弃的部分,10.1.10也能正确执行。但是不兼容的情况也会发生。

bundle exec就是为了解决这样的问题而存在的:在目前的Bundle环境里执行某个操作,这样对于不同的人来说,不论系统里是什么版本的Gem,总是会使用该项目Gemfile中指定的版本来执行某个操作。


binstubs
有些环境,使用bundle exec太复杂。此时可以使用
$ bundle install --binstubs
之后就可以使用bin/rails db:migrate

bundle install -h
查看相关信息。

作者不是很喜欢gem 'simple_form',不过能节约时间和减少一些trouble.

https://github.com/plataformatec/simple_form (7000✨)


 

Form 

 

使用scaffold建立基本的结构。

然后根据需要添加不同的FormtagHelper,或者FormHelper

两者的区别,tagHelper需要手动添加name和value。FormHelper需要分配一个ActiveRecord对象。

 



 

Cookies 

具体用法查阅API. 

 

通过cookie,你可以储存信息在浏览器上,以key/value的形式,格式是string。cookie是web server之前发送给浏览器的。

之后,当浏览发出request时,cookie存在HTTP header中,会从浏览器发送到服务器。

 https://en.wikipedia.org/wiki/HTTP_cookie

 

一个cookies限定4kb。一般只能储存ID。

Rails提供一个hash,cookies[]。 

例子:

class HomeController < ApplicationController
  def set_cookies
    cookies[:user_name] = "Smith"
    cookiesp[:customer_number] = "123456"
  end
  def show_cookies
    @user_name = cookies[:user_name]
    @customer_number = cookies[:customer_number]
  end
  def delete_cookies
    cookies.delete :user_name
    cookies.delete :customer_number
  end
end
在views/home/show_cookies.html.erb中,
<table>
  <tr>
    <td>User Name:</td>
    <td><%= @user_name %></td>
  </tr>
  <tr>
    <td>Customer Number:</td>
    <td><%= @customer_number %></td>
  </tr>
</table>
先进http://localhost:3000/home/set_cookies,控制器set_cookies动作生产了cookies。

再进 http://localhost:3000/home/show_cookies,可以看到cookies的值了。

如果进入http://localhost:3000/home/delete_cookies 会删除cookies  

 

cookies[]有value选项,expires选项,等等具体见API。

# Sets a cookie that expires in 1 hour.
cookies[:login] = { value: "XJ-122", expires: 1.hour }

 

permanent():

cookies.permanent[:user_id] = "chen"

设置到期时间是20年。 

 

Signed Cookies 

config/secrets.yml这个机制好像5.2删除了。


 

Sessions 

 

独立的web page之间没有关联。如果一个用户想要只注册一次就可以在网站上随意浏览。那么就需要用到session[] hash机制。 

Rails创建一个新的session为每个访问这个web page的人。默认session被保存到cookie。也可以把它储存在数据库。 一个独立的唯一的session🆔也被自动创建。

Rails session可以保存任意对象。 

session[]需要developer自己设置。

 

reset_session方法,会删除当前的session并开启一个新的session。

 

rails g controller Home reset -s 

# 之前已经建立了Home控制器,这是快捷的添加reset方法的相关文件,如view,helper,route, test, 参数-s会忽略之前建立的文件。 

 

Saving Sessions in the Database 

可以把session主体放到数据库中,cookie中只保留session的🆔。

一个现成的gem: https://github.com/rails/activerecord-session_store 

❌但是有问题。directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for: 

 

posted @ 2018-06-18 14:39  Mr-chen  阅读(260)  评论(0编辑  收藏  举报