我的用例很简单,在一个名为dashboard/incomes
I display a form to update a record of type的页面中IncomeSetting
。
#income-setting-form
h4 Income Settings
p Please set your Income Settings using the form below.
= render 'income_settings/form'
这将生成用于编辑此类对象的表单:
= simple_form_for @income_setting do |f|
= f.hidden_field :user_id
= f.error_notification
.form-group
= f.label :amount
= f.input_field :amount, required: true, class: 'form-control'
= f.error :amount, id: 'amount_error'
= f.association :income_frequency_type, label: 'Frequency:', collection: IncomeFrequencyType.order('id ASC'), include_blank: false, wrapper_html: { class: 'form-group' }, input_html: { class: 'form-control' }
.form-group
= f.label :start_date
= f.input_field :start_date, required: true, as: :string, class: 'form-control datepicker'
= f.error :start_date, id: 'start_date_error'
= f.association :savings_rate_type, label: 'Savings Rate:', collection: SavingsRateType.order('name ASC'), include_blank: false, wrapper_html: { class: 'form-group' }, input_html: { class: 'form-control' }
.form-group
= f.label :description
= f.input_field :description, required: true, class: 'form-control'
= f.error :description, id: 'amount_error'
button.btn.btn-primary.btn-block type='submit' Save
为了保持 RESTful 和易于维护,我决定将IncomeSetting
对象的所有操作都保存在income_settings_controller.rb
文件中。
def update
if @income_setting.update(income_setting_params)
redirect_to dashboard_income_path, notice: 'Your Income Setting was saved successfully updated.'
else
redirect_to controller: 'dashboard', action: 'income'
end
end
您是否看到验证失败的地方,我重定向到仪表板?如果我在那里放置一个断点,我可以看到模型@income_setting
确实存在验证错误 - 但就像控制器重定向模型错误一样丢失?
关于如何持久化这些错误以便它们在render 'income_settings/form
调用时实际显示的任何建议?