2013-03-04 44 views
2

我是Rails3的新手,並且構建了非常簡單的應用程序(我不想要AJAX)。無論如何,我有indexcreate的行動。該index動作包含窗體:Google Chrome瀏覽器 - 防止在Rails應用程序刷新時重新提交表單

<%= 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 

編輯:這個問題似乎是在谷歌瀏覽器。

+1

我在這裏沒有看到任何錯誤,您是否嘗試過使用不同的瀏覽器? – Intrepidd 2013-03-04 19:49:14

+0

是的。基本上,我在'GET promote_index',其中包含'POST promote_index'的表單,它將我重定向到'GET promote_index'。但是當我在重定向後打F5時,它又想重新提交表單。 – 2013-03-04 19:54:20

+0

我不認爲你需要按鍵閃光燈,''redirect_to promote_index_url,注意:「促銷已經開始」'(不是說這是錯誤) – Leito 2013-03-04 20:31:42

回答

2

我有同樣的問題,並最終跟蹤它到一個bug在Chrome(https://code.google.com/p/chromium/issues/detail?id=177855

我周圍的工作中使用的只是添加一些獲取參數。以下是我處理錯誤和挫折的方式。結果可能不適用於所有情況。幸運的是我的應用是內部的。

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比賽路線似乎是多餘的與資源路線指標的行動。

+0

雅。當然可以少些羅嗦。 – 2013-03-06 17:37:16

+0

謝謝!希望Chrome即將修補。 – 2013-03-06 19:15:04

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 
+0

它不起作用:( – 2013-03-04 22:28:22

相關問題