Rails:恢复使用form_tag的非模型形式的内容 [英] Rails: Restoring contents of non-model form that uses form_tag
问题描述
我正在使用form_tag,而在成功的情况下,一切都表现良好。但是,处理错误有些不友好。实际的错误消息存储在Flash中并通过layout / application.html显示,但如果表单可以记住用户刚刚填写的内容,我真的很喜欢它,但是它不是:所有的字段重设为默认值。
我喜欢表单对于对象的RESTful操作的方式会自动记住其提交的值,如果有错误,则会以红色突出显示。我没有红色的亮点是好的,但如果我可以让表单的字段保留提交的值,我真的很喜欢。
任何人都可以建议如何做?
相关文件摘录:
views / cardsets / import.html.erb:
<%= form_tag:action => :import_data,:id => @cardset do%>
...
<%= text_field_tagseparator,,maxlength => 1%>
...
<%= text_field_tagformatting_line%>
...等(更多字段)
controllers / cardsets_controller.rb:
#POST / cardsets / 1 / import_data
def import_data
success,message = @ cardset.import_data(params, current_user)
如果成功
redirect_to(@cardset,:notice => message)
else
flash.now [:error] = message
render:import
end
end
第二个arg_text_field_tag是用于填充该字段的值。尝试这样的东西:
<%= text_field_tagseparator,params [:separator],:maxlength => 1%>
I'm making good progress with my first Rails app (using Rails 3). The MVC interaction is all going fine, but I'm having difficulty with a form that doesn't relate directly to a model.
I'm using form_tag, and in the case of success, everything behaves fine. However, the handling of errors is somewhat unfriendly. The actual error message is stored in the flash and displayed fine by layouts/application.html, but I'd really like it if the form could remember the contents that the user had just filled in. But it doesn't: all the fields reset to their default values.
I love the way that forms for RESTful actions on objects automatically remember their submitted values, and get highlighted in red if there are errors. I'm fine without the red highlight, but I'd really like it if I could make the form's fields keep the submitted values.
Can anyone advise how to do this?
Excerpts from the relevant files:
views/cardsets/import.html.erb:
<%= form_tag :action => :import_data, :id => @cardset do %>
...
<%= text_field_tag "separator", "", :maxlength => 1 %>
...
<%= text_field_tag "formatting_line" %>
...etc (more fields)
controllers/cardsets_controller.rb:
# POST /cardsets/1/import_data
def import_data
success, message = @cardset.import_data(params, current_user)
if success
redirect_to(@cardset, :notice => message)
else
flash.now[:error] = message
render :import
end
end
The second arg to text_field_tag is the value to fill in the field with. Try something like this:
<%= text_field_tag "separator", params[:separator], :maxlength => 1 %>
这篇关于Rails:恢复使用form_tag的非模型形式的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!