谷歌浏览器 - 在Rails应用程序中防止刷新时重新提交表单

时间:2013-03-04 19:14:12

标签: ruby-on-rails ruby-on-rails-3

我是Rails3的新手,我正在构建一个非常简单的应用程序(我不想要AJAX)。无论如何,我有indexcreate行动。 index操作包含表单:

<%= form_for @message, url: promote_index_path do |f| %>
 ...
<% end %>

我的promote_controller看起来像这样:

  def index
    @message = Message.new url: @last_video, message_body: @last_message
  end

  def create
    @message = current_user.messages.new(params[:message])
    @message.get_video_id
    if @message.save
      redirect_to promote_index_url, flash: {notice: "Promotion has been started"}
    else
      render 'index'
    end
  end

这一切都正常,除非成功提交表单,并且通知闪烁,如果用户点击F5,表单将重新提交。不应该是这种情况,因为我在成功保存后正在进行redirect_to。我相信我在这里做了一些根本性的错误,所以请帮助我。

编辑: 这是我的routes.rb

 resources :home
  resources :find
  resources :promote
  resources :settings
  root to: 'home#index'
  match 'auth/:provider/callback', to: 'sessions#create'
  match 'auth/failure', to: redirect('/')
  match 'logout', to: 'sessions#destroy', as: 'logout'
  match 'find', to: 'find#index', as: 'find'
  match 'promote', to: 'promote#index', as: 'promote'
  match 'settings', to: 'settings#index', as: 'settings'

这是我的rake routes

Jans-MacBook-Pro-2:tp3 jan$ rake routes
settings_index GET    /settings/index(.:format)          settings#index
    home_index GET    /home(.:format)                    home#index
               POST   /home(.:format)                    home#create
      new_home GET    /home/new(.:format)                home#new
     edit_home GET    /home/:id/edit(.:format)           home#edit
          home GET    /home/:id(.:format)                home#show
               PUT    /home/:id(.:format)                home#update
               DELETE /home/:id(.:format)                home#destroy
    find_index GET    /find(.:format)                    find#index
               POST   /find(.:format)                    find#create
      new_find GET    /find/new(.:format)                find#new
     edit_find GET    /find/:id/edit(.:format)           find#edit
          find GET    /find/:id(.:format)                find#show
               PUT    /find/:id(.:format)                find#update
               DELETE /find/:id(.:format)                find#destroy
 promote_index GET    /promote(.:format)                 promote#index
               POST   /promote(.:format)                 promote#create
   new_promote GET    /promote/new(.:format)             promote#new
  edit_promote GET    /promote/:id/edit(.:format)        promote#edit
       promote GET    /promote/:id(.:format)             promote#show
               PUT    /promote/:id(.:format)             promote#update
               DELETE /promote/:id(.:format)             promote#destroy
      settings GET    /settings(.:format)                settings#index
               POST   /settings(.:format)                settings#create
   new_setting GET    /settings/new(.:format)            settings#new
  edit_setting GET    /settings/:id/edit(.:format)       settings#edit
       setting GET    /settings/:id(.:format)            settings#show
               PUT    /settings/:id(.:format)            settings#update
               DELETE /settings/:id(.:format)            settings#destroy
          root        /                                  home#index
                      /auth/:provider/callback(.:format) sessions#create
  auth_failure        /auth/failure(.:format)            :controller#:action
        logout        /logout(.:format)                  sessions#destroy
          find        /find(.:format)                    find#index
       promote        /promote(.:format)                 promote#index
      settings        /settings(.:format)                settings#index

编辑:问题似乎出现在谷歌浏览器中。

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题并最终将其追踪到Chrome中的错误(https://code.google.com/p/chromium/issues/detail?id=177855

我使用的工作只是添加一些get参数。这是我如何处理错误和挫折。结果可能不适合所有方案。幸运的是我的应用是内部的。

def create
  @message = current_user.messages.new(params[:message])
  @message.get_video_id
  if @message.save
    flash[:notice] = "Promotion has been started"
    redirect_to promote_index_url(chrome_bug_workaround: 177855)
  else
    render 'index'
  end
end

另一种方法是创建一个基本上是索引别名的收集路径。

routes.rb

match 'promote/all' => 'promote#index', :as => 'all_promotions'

promote_controller.rb

def create
  @message = current_user.messages.new(params[:message])
  @message.get_video_id
  if @message.save
    redirect_to all_promotions_url, flash: {notice: "Promotion has been started"}
  else
    render 'index'
  end
end

另外,您的findpromotesettings匹配路由似乎与资源路由的索引操作是多余的。

答案 1 :(得分:0)

尝试将您的方法更改为:

def index
    @message = Message.new url: @last_video, message_body: @last_message

    respond_to do |format|
      format.html
    end
  end

  def create
    @message = current_user.messages.new(params[:message])
    @message.get_video_id

    respond_to do |format|
      if @message.save
        format.html {redirect_to promote_index_url, notice: "Promotion has been started"}
      else
        render 'index'
      end
    end
  end