当表单被第二次加载时保持表单数据
问题描述:
我有一个从那做两件事。当表单被第二次加载时保持表单数据
获取用户文本输入。
用户可以选择一张图片或上传自己的图片。
用户上传图片时发生问题。
文件字段必须提交信息才能上传图像。它完成所有工作。
但是当窗体与上传的图像一起显示时,用户输入的文本消失了。我知道实例变量不会保留数据。
如何使这项工作,使文本不会丢失? 我必须使用AJAX吗?
预先感谢您。
答
如果你正在使用的form_for,
# your_controller.rb
def new # method where you show the form
@some_model = SomeModel.new(params[:some_model])
end
# new.html.haml - i like haml
- form_for @some_model do
... # all your inputs (they will be pre-set if you submit the form and it stays in new method
如果你正在使用的form_tag
# new.html.haml
- form_tag do
= text_field_tag :some_attribute_name, params[:some_attribute_name]
= select_tag :another_attribute_name, options_for_select(['option1', 'option2', 'and so on'], params[:another_attribute_name])
所以要看你使用的是什么时,选择相应的方式;-)希望这有助于=)
但是
我认为图像应该与表格一起提交并保存t一共,也就是说它不应该是一个单独的过程,除非你正在谈论表单被保存并且你正在重定向到编辑页面......无论哪种方式,这两种方法都应该工作=)
你使用表单模型的助手(例如, form_for:somemodel),还是使用普通的form_tags? – Staelen 2011-03-01 07:57:08