简体   繁体   English

在Rails应用中的控制器之间共享变量

[英]sharing variables between controller an view in a rails app

in my controller I am collecting data (into a hash) like this (note that I do not have a BillingAddress model in my app, @billing_address is standard ruby hash 在我的控制器中,我正在像这样收集数据(到哈希中)(请注意,我的应用程序中没有BillingAddress模型, @billing_address是标准的ruby哈希

@billing_address = params[:billing_address]

my view is laid out like this 我的观点是这样布置的

<%= text_field_tag 'billing_address[phone]' %>

I want to show the previous value that user entered (in case of errors), like this: 我想显示用户输入的先前值(以防出现错误),如下所示:

<%= text_field_tag 'billing_address[phone]', @billing_address['phone'] %>

however, this gives me an exception saying I am trying to access nil, ideas? 但是,这给了我一个例外,说我正尝试使用nil,想法吗?

I guess params[:billing_address] is nil . 我猜想params[:billing_address]nil
Try to assign empty hash if it is. 尝试分配空散列(如果有)。

@billing_address = params[:billing_address] || {}

This is not the Rails way of showing previous values in case of error. 如果出现错误,这不是Rails显示先前值的方式。 Check this screencast to get idea on how to handle errors better way. 检查此截屏视频,以了解如何更好地处理错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM