4-8,4-9

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  # 在数据被创建,更新,或删除后after_commit(*arg, &block)
  # 还 不理解
  after_commit { CommentRelayJob.perform_later(self) }
end


<section id="comments" data-channel="comments" data-message-id="<%= message.id %>">

这两个属性来自哪里? 

  <%= render message.comments %>
</section>

section:是html元素,一般用于内加一个标题<h1>和<p>。

html Global attribute (公共属性):如 id,  data_*


data_*:   <element data-*="somevalue">  (点击看实际例子)

Use the data-* attribute to embed custom data:
 

Definition and Usage

The data-* attributes is used to store custom data private to the page or application.

The data-* attributes gives us the ability to embed custom data attributes on all HTML elements.

The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).

The data-* attributes consist of two parts:

  1. The attribute name should not contain any uppercase letters, and must be at least one character long after the prefix "data-"
  2. The attribute value can be any string

<%= form_for [message, Comment.new], remote: true do | form| %>
  <%= form.text_area :content, size: '100x20' %> <br>
  <%= from.submit "Post comment"%>
<% end %>

 解释1:表单可以传入关联的数据。

If your resource has associations defined, for example, you want to add comments to the document given that the routes are set correctly:

<%= form_for([@document, @comment]) do |f| %>  ... <% end %>

Where @document = Document.find(params[:id]) and@comment = Comment.new.

 解释2: submit->submit_tag 提交关联到Comment#create方法。 

  def create
    @comment = Comment.create!(
      content: params[:comment][:content],
      message: @message,
      user: @current_user
    )
  end

 4-9 上午练习了案例的结构

Rails-Guide: 12.6  cookies  (内含API)

 用法类似session.

删除cookies,使用cookies.delete(:key),  而删除session中的数据则是键的值设为nil

 The cookies being read are the ones received along with the request, the cookies being written will be sent out with the response. Reading a cookie does not get the cookie object itself back, just the value it holds.

webserver接收request 时 , cookies being read ;发送response前,cookies being written。


# Sets a simple session cookie.
# This cookie will be deleted when the user's browser is closed.
cookies[:user_name] = "david"

# Cookie values are String based. Other data types need to be serialized.
cookies[:lat_lon] = JSON.generate([47.68, -122.37])
JSON.parse(cookies[:lat_lon]) # => [47.68, -122.37]
# Sets a cookie that expires in 1 hour.设定一个小时内有效的cookie cookies[:login] = { value: "XJ-122", expires: 1.hour.from_now }

签名cookie,用于储存敏感数据。在cookie的值后面加一个签名,确保值没有被修改。

# Sets a signed cookie, which prevents users from tampering with its value.
# The cookie is signed by your app's `secrets.secret_key_base` value.
# It can be read using the signed method `cookies.signed[:name]`
cookies.signed[:user_id] = current_user.id

加密的cookie。加密后终端无法读取。

# Sets an encrypted cookie value before sending it to the client which
# prevent users from reading and tampering with its value.
# The cookie is signed by your app's `secrets.secret_key_base` value.
# It can be read using the encrypted method `cookies.encrypted[:name]`
cookies.encrypted[:discount] = 45

可以把以上方法一起用:比如先签名后再加密。

# You can also chain these methods(设定有效期20年的cookie,permanent):
cookies.permanent.signed[:login] = "XJ-122" 
原文地址:https://www.cnblogs.com/chentianwei/p/8747335.html