开发者

Rails: Generic form actions, cancel link losing `:back` on validation failure

I am trying to create a generic set of Submit, Cancel, and Destroy actions for forms. At this point, it appears that everything is working, except that I lose :back functionality then a form reloads due to validation errors. Is there a way to catch the fact that validation has failed, and in that case, keep the request.env['HTTP_REFERER'] or :back value the same without hav开发者_如何学Pythoning to edit every controller?

= 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


How about something like a helper method with something like this?

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

Then the Cancel button is just:

link_to('Cancel', @previous)


Does this work?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜