2011-03-01 71 views
0

我有一個從那做兩件事。當表單被第二次加載時保持表單數據

  1. 獲取用戶文本輸入。

  2. 用戶可以選擇一張圖片或上傳自己的圖片。

用戶上傳圖片時發生問題。

文件字段必須提交信息才能上傳圖像。它完成所有工作。

但是當窗體與上傳的圖像一起顯示時,用戶輸入的文本消失了。我知道實例變量不會保留數據。

如何使這項工作,使文本不會丟失? 我必須使用AJAX嗎?

預先感謝您。

+0

你使用表單模型的助手(例如, form_for:somemodel),還是使用普通的form_tags? – Staelen 2011-03-01 07:57:08

回答

1

如果你正在使用的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一共,也就是說它不應該是一個單獨的過程,除非你正在談論表單被保存並且你正在重定向到編輯頁面......無論哪種方式,這兩種方法都應該工作=)

相關問題