如何将会话中的数据保存到数据库?

发布于 2024-11-08 02:25:07 字数 1901 浏览 1 评论 0原文

我正在使用 Rails 3 应用程序。 我有一个问题。如何将会话中的数据保存到数据库中?我想使用日期时间数据类型将签入和签出会话保存到数据库中。

控制器:

def step2
    @cart = current_cart

    checkin = params[:checkin]
    checkout = params[:checkout]
    @amenities = Amenity.available(checkin, checkout)

    session[:checkin] = checkin
    session[:checkout] = checkout
end

视图:

<%= f.hidden_field :Transaction_Date, :value => Time.now %>
    <%= f.hidden_field :rsv_type, :value => "Online" %>
    <%= f.hidden_field :rsv_status, :value => "Pending" %>
    <%= f.hidden_field :checkin, session[:checkin] %>
    <%= f.hidden_field :checkout, session[:checkout] %>
<% end %>

我收到此错误:

NoMethodError
undefined method `merge' for "05/18/2011":String

请帮忙。谢谢。

来自我的应用程序控制器:

private

def current_cart
    Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
    cart = Cart.create
    session[:cart_id] = cart.id
    cart
end

当我输入这些代码时:

@cart.checkin = DateTime.strptime(session[:checkin], "%m/d/%Y %I:%M:%S %p").to_time
@cart.checkout= DateTime.strptime(session[:checkout], "%m/d/%Y %I:%M:%S %p").to_time
@cart.save

我收到 ArgumentError invalid date 错误。 :( 我应该做什么?我的数据库使用 sql server 2005。

这是我的购物车模型

类 Cart < ActiveRecord::Base has_many :line_items, :dependent =>; :破坏

def add_amenity(amenity_id)
    current_item = line_items.where(:amenity_id => amenity_id).first
    if current_item
        return false
    else            
        current_item = LineItem.new(:amenity_id => amenity_id)
        line_items << current_item
    end
    current_item
end

def total_price
    line_items.to_a.sum { |item| item.amenity.fee }
end

def payable
    total_price * 0.50
end
end

I'm using rails 3 app.
I have a problem. How will I save data in my session to my database? I want to save the checkin and checkout session to the database with a datetime data type.

controller:

def step2
    @cart = current_cart

    checkin = params[:checkin]
    checkout = params[:checkout]
    @amenities = Amenity.available(checkin, checkout)

    session[:checkin] = checkin
    session[:checkout] = checkout
end

view:

<%= f.hidden_field :Transaction_Date, :value => Time.now %>
    <%= f.hidden_field :rsv_type, :value => "Online" %>
    <%= f.hidden_field :rsv_status, :value => "Pending" %>
    <%= f.hidden_field :checkin, session[:checkin] %>
    <%= f.hidden_field :checkout, session[:checkout] %>
<% end %>

im getting this error:

NoMethodError
undefined method `merge' for "05/18/2011":String

help.. please. thanks.

from my applicationcontroller:

private

def current_cart
    Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
    cart = Cart.create
    session[:cart_id] = cart.id
    cart
end

when i put these code:

@cart.checkin = DateTime.strptime(session[:checkin], "%m/d/%Y %I:%M:%S %p").to_time
@cart.checkout= DateTime.strptime(session[:checkout], "%m/d/%Y %I:%M:%S %p").to_time
@cart.save

i get an error of ArgumentError invalid date. :( what should i do? im using a sql server 2005 for my database.

here's my cart model

class Cart < ActiveRecord::Base
has_many :line_items, :dependent => :destroy

def add_amenity(amenity_id)
    current_item = line_items.where(:amenity_id => amenity_id).first
    if current_item
        return false
    else            
        current_item = LineItem.new(:amenity_id => amenity_id)
        line_items << current_item
    end
    current_item
end

def total_price
    line_items.to_a.sum { |item| item.amenity.fee }
end

def payable
    total_price * 0.50
end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日裸衫吸 2024-11-15 02:25:07

您不需要在表单中传递会话数据(示例中的最后两个隐藏字段)。一般来说,保存会话数据非常简单。

# some controller action with @cart defined
@cart.checkin = session[:checkin]
@cart.checkout = session[:checkout]
@cart.save

确保对此会话数据进行任何必要的验证,以保护您的系统并确保保存到数据库的数据是高质量的。

另外,会话数据不应该被信任为关键信息,并且绝对不要使用它来存储密码、与购物车相关的财务数据等。


关于问题的日期部分,这是如何转换字符串日期到实际日期/时间。

DateTime.strptime("2011/05/18 13:42:02", "%Y/%m/%d %H:%M:%S").to_time

You don't need to pass session data in your form (the last two hidden_fields in your example). In general, saving your session data is very simple.

# some controller action with @cart defined
@cart.checkin = session[:checkin]
@cart.checkout = session[:checkout]
@cart.save

Make sure to include any necessary validations on this session data to protect your system and to ensure that data saved to your database is high quality.

Also, session data shouldn't be trusted for critical information, and definitely don't use it to store passwords, financial data related to your shopping cart, etc.


Regarding the date part of your question, this is how to convert a string date to an actual date/time.

DateTime.strptime("2011/05/18 13:42:02", "%Y/%m/%d %H:%M:%S").to_time
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文