Rails:通用表单操作,取消链接在验证失败时丢失`:back`

时间:2011-01-16 21:36:54

标签: ruby-on-rails

我正在尝试为表单创建一组通用的提交,取消和销毁操作。此时,似乎一切正常,但我失去了:back功能,然后由于验证错误而重新加载表单。有没有办法捕获验证失败的事实,在这种情况下,保持request.env['HTTP_REFERER']:back值不变,而不必编辑每个控制器?

= simple_form_for @announcement do |f|
  = f.error_notification
  = f.input :message
  = f.input :starts_at
  = f.input :ends_at
  #submit
    = f.button :submit
    = "or "
    = link_to("cancel", url_for(:back))
    .right
      - if !f.object.new_record?
        - resource = (f.object.class.name).downcase
        = link_to "destroy", url_for(:action => 'destroy'), :confirm => "Are you sure that you want to delete this #{resource}?", :method => :delete
  .clear
  .non_input
    #post_back_msg
      #indicator.inline
        = image_tag "indicator.gif"
      .inline
        = "Please wait..."
      .non_input

2 个答案:

答案 0 :(得分:0)

像这样的帮手方法怎么样?

@previous = @previous.blank? ? request.env['HTTP_REFERRER'] : @previous

然后取消按钮只是:

link_to('Cancel', @previous)

答案 1 :(得分:0)

这有用吗?

link_to "Cancel", @model.errors.any? ? request.env['HTTP_REFERRER'] : :back