如果一个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

Learn Rails5.2 Bundler ; Forms的更多相关文章

  1. Learn Rails5.2- ActiveRecord: Migration , spring的使用(不兼容的解决办法)

    偶然一次: 运行rails generate停止不动,网上查找答案,可能是bundle update 之后 spring 版本变化了,和正在运行的 spring 实例不兼容. Spring导致的同样的 ...

  2. Learn Rails5.2-- rails base(含官方指导Debugging 摘录)

    豆知识扩展: <meta>  https://www.cnblogs.com/chentianwei/p/9183799.html css selector div > p 选择所有 ...

  3. Learn Rails5.2 Routes。( 很少用到的参数:constraints和redirect)

    Naming a Route get 'home/index', as: "different_name" 会得到prefix: different_name代替home_inde ...

  4. Learn Rails5.2- Scaffolding and REST,flash.now, flash.keep; Access via JSON

    用generator建立一个手脚架 Representational State Transfer (REST).  具像的状态转存. https://en.wikipedia.org/wiki/Re ...

  5. Learn Rails5.2- ActiveRecord: sqlite3的用法, Query查询语法。乐观锁和悲观锁案例,查询语法includes(), 多态关联,destory和delete, Scope, Validats, Migrations

    rails generate model photo title:string album:references 这会产生一个album_id列,当建立belongs_to关联时,需要用到. refe ...

  6. 3 Suggested Oracle Certifications For Oracle Form's Developers

    The following are the most suggested Oracle Certifications for Oracle Application Developers in Form ...

  7. Learn How To Create Trigger In Oracle Forms

    I have written many posts related to triggers in Oracle Forms, I have given examples for Form Level ...

  8. Learn How To Attach PL/SQL Library In Oracle Forms

    To attach a PL/SQL library in the Oracle Forms follow the following steps:1. Click on Attached Libra ...

  9. Xamarin.Forms介绍

    On May 28, 2014, Xamarin introduced Xamarin.Forms, which allows you to write user-interface code tha ...

随机推荐

  1. postgresql----LIKE和SIMILAR TO

    LIKE和SIMILAR TO都支持模糊查询,另外SIMILAR TO还支持正则表达式查询.模糊查询中有两个重要的符号:下划线'_'匹配任意单个字符,百分号'%'匹配任意多个字符,可以是0个,如果想匹 ...

  2. 朴素贝叶斯算法的python实现 -- 机器学习实战

    import numpy as np import re #词表到向量的转换函数 def loadDataSet(): postingList = [['my', 'dog', 'has', 'fle ...

  3. Redis 缓存穿透,缓存击穿,缓存雪崩的解决方案分析

    设计一个缓存系统,不得不要考虑的问题就是:缓存穿透.缓存击穿与失效时的雪崩效应. 一.什么样的数据适合缓存? 分析一个数据是否适合缓存,我们要从访问频率.读写比例.数据一致性等要求去分析.  二.什么 ...

  4. ubuntu 下安装 jdk

    1. 下载 jdk : https://www.oracle.com/technetwork/java/javase/downloads/index.html 2. 解压 jdk 到系统默认 jdk ...

  5. paramiko与ssh

    一.paramiko模块的安装 paramiko模块依赖PyCrypto模块,而PyCrypto需要GCC库编译,不过一般发行版的源里带有该模块.这里以centos6为例,直接借助以下命令可以直接完成 ...

  6. Linux内核之vmlinux与vmlinuz

    因为是初次系统的学习Linux内核,过程中遇到了一些常常出现的名词.似曾相识,但对他们的含义又不是非常清楚.因此,将搜索到的内容进行一下汇总. 1.vmlinux   vmlinux是一个包括linu ...

  7. centos linux系统日常管理3 服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,curl,ping ,telnet,traceroute ,dig ,nc,nmap,host,nethogs 第十六节课

    centos linux系统日常管理3  服务管理ntsysv,chkconfig,系统日志rsyslog,last ,lastb ,exec,xargs,dmesg,screen,nohup,cur ...

  8. Spring Boot 全局异常配置

    Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中.使用 ControllerAdvice 注解 package com.li.controller; import o ...

  9. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

  10. JS获取客户端系统当前时区

    <script> function getClientTimezone(){ var oDate = new Date(); var nTimezone = -oDate.getTimez ...