我在我的Rails 4應用程序中使用Simple_form。如何使用Simple_form但沒有模型顯示驗證錯誤消息?
如何在與模型無關的視圖中顯示錯誤消息?
我想要得到與基於模型的其他視圖相同的結果。
現在,這是在視圖的代碼:
<%= simple_form_for(:registration, html: { role: 'form' }, :url => registrations_path) do |f| %>
<%= f.error_notification %>
<%= f.input :name, :required => true, :autofocus => true %>
<%= f.input :email, :required => true %>
<%= f.input :password, :required => true %>
<%= f.input :password_confirmation, :required => true %>
<%= f.button :submit %>
<% end %>
在「正常」的視圖(即,與一個模型)的線<%= f.error_notification %>
顯示錯誤。
我應該怎樣在我的控制器中初始化Simple_form使用的內容來顯示錯誤?
感謝
謝謝你的回答。可以在不使用模型的情況下使用Simple_form:只需使用'':'symbol''''作爲第一個參數。實際上,我的控制器/視圖工作得很好,我只想顯示基於模型的視圖(由我的控制器生成)作爲視圖的錯誤消息。無論如何,感謝您的想法,我會看看這個解決方案。 –
啊,很高興知道。沒有意識到關於simple_form。不過,'error_notification'助手很可能在包裝對象上尋找'errors'方法。它是有道理的,一個符號不會對此作出迴應。我想知道你是否可以用一個''Struct'對象來響應'errors'。也許,但它仍然像是一個非數據庫支持的模型對我來說會更好。 – pdobb