RailsCasts13 Dangers of Model in Session 将model放在session中是危险的

在此介绍为什么将model放在session中是一个坏注意。

In this episode we’ll show you why it’s a bad idea to store a model in the session.

ruby

class UserController < ApplicationController
  def prepare
    session[:user] = User.find(:first)
    redirect_to :action => :'show'
  end

  def show
    @user = session[:user]
  end

  def update
    @user = session[:user]
    @user.name = 'Foo'
    redirect_to :action => 'show'
  end  
end

UserController中的prepare将一个model存储在了session中。

上面的代码片段中,控制器有3个action。prepare action查找出第一个user,将其存储在session中,然后跳转至show。show action从session中得到当前的用户并输出一些user的debug信息,show view代码如下。

<%= debug(@user) %>
<%= link_to 'update', :action => 'update' %>

show页面中有一个update的链接。update再次从session中得到当前user,改变它的一个特性并show出来;在update中的更改只是临时的,未被写到数据库中;最终,update跳转至show,如下图

update后的show,显示出的数据与数据库中的数据是不同步的,数据库中的数据如下:

terminal

NooNoo:ep13 eifion$ script/console
Loading development environment (Rails 2.2.2)
>> User.first.name
=> "Eifion" 
>>

数据库中,user的name仍为‘Eifion’.


验证中出现的问题:

在User模型中,对name存在非空校验(with validates_presence_of).更新update action将name设置成空字符串,校验user是否有效,结果如下:

ruby

def update
  @user = session[:user]
  @user.name = ''
  @user.valid?
  redirect_to :action => 'show'
end

将name置成空时,会抛出校验错误;但当跳转到其他页面时,错误信息不能被保存。

正确的方式

将user_id存储在session中,controller中各个action的代码如下:

ruby

class UserController < ApplicationController
  def prepare
    session[:user_id] = User.find(:first).id
    redirect_to :action => :'show'
  end

  def show
    @user = User.find(session[:user_id])
  end

  def update
    @user = User.find(session[:user_id])
    @user.name = 'Foo'
    redirect_to :action => 'show'
  end  
end
现将user_id存储在session中,每次请求均从数据库中获取数据。此时,user name的变化不会被持续的保留。

session应仅被用来存储简单的对象,如array, strings and integers.


原文地址:http://railscasts.com/episodes/13-dangers-of-model-in-session?view=asciicast


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
With the rapid development of China's economy, the per capita share of cars has rapidly increased, bringing great convenience to people's lives. However, with it came a huge number of traffic accidents. A statistical data from Europe shows that if a warning can be issued to drivers 0.5 seconds before an accident occurs, 70% of traffic accidents can be avoided. Therefore, it is particularly important to promptly remind drivers of potential dangers to prevent traffic accidents from occurring. The purpose of this question is to construct a machine vision based driving assistance system based on machine vision, providing driving assistance for drivers during daytime driving. The main function of the system is to achieve visual recognition of pedestrians and traffic signs, estimate the distance from the vehicle in front, and issue a warning to the driver when needed. This driving assistance system can effectively reduce the probability of traffic accidents and ensure the safety of drivers' lives and property. The main research content of this article includes the following aspects: 1. Implement object detection based on the YOLOv5 model. Conduct research on convolutional neural networks and YOLOv5 algorithm, and develop an object detection algorithm based on YOLO5. Detect the algorithm through road images, and analyze the target detection algorithm based on the data returned after training. 2. Estimate the distance from the front vehicle based on a monocular camera. Study the principle of estimating distance with a monocular camera, combined with parameters fed back by object detection algorithms, to achieve distance estimation for vehicles ahead. Finally, the distance estimation function was tested and the error in the system's distance estimation was analyzed. 3. Design and implementation of a driving assistance system. Based on the results of two parts: target detection and distance estimation, an intelligent driving assistance system is constructed. The system is tested through actual road images, and the operational effectiveness of the intelligent driving assistance system is analyzed. Finally, the driving assistance system is analyzed and summarized.
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值