如果一个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. 自定义Realm解析

    自定义Realm解析---------------------------------------> /* * Copyright 2005-2013 shopxx.net. All right ...

  2. Xshell连接阿里云Centos6.8

    由于我将xshell更新到了Xshell v5.0 Build 1332,在连接阿里云服务器的时候遇到了一些问题. 以前我登录到服务器的时候直接输入登录密码就行了,但是现在由于openssh服务器类型 ...

  3. 【Python项目篇】【爬妹子图】

    #-*- coding:utf-8 -*- import urllib import urllib2 from bs4 import beautifulsoup4 #获取标签下的内容 #打开网页,获取 ...

  4. Openstack(三)Haproxy+Keepalived双机

    3.1部署keepalived 3.1.1下载keepalived源码包,并解压 # wget http://www.keepalived.org/software/keepalived-1.4.2. ...

  5. Selenium之firefox浏览器的启动问题及解决

    启动firefox报错如下: rg.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 ...

  6. google chrome插件开发,自己动手,丰衣足食

    因为平时上网都用chrome,但总感觉除了速度快,简洁以外总还有地方满足不了我的需要,然后找插件…后来发现,插件虽然海量但找个称心如意的也不是件容易的事儿,用在找插件的时间都能自己写一个了,于是,今年 ...

  7. Java基础知识 Set

    在Java中使用Set,可以方便地将需要的类型以集合类型保存在一个变量中.主要应用在显示列表.Set是一个不包含重复元素的 collection. 更确切地讲,set 不包含满足 e1.equals( ...

  8. Flask上下文管理及源码刨析

    基本流程概述 - 与django相比是两种不同的实现方式. - django/tornado是通过传参数形式实现 - 而flask是通过上下文管理, 两种都可以实现,只不实现的方式不一样罢了. - 上 ...

  9. 7.3 Models -- Creating And Deleting Records

    一.Creating 1. 你可以通过调用在store中的createRecord方法来创建records. store.createRecord('post', { title: 'Rails is ...

  10. Java基础教程:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...