2012-01-18 82 views
0

控制器動作對於button_to應該是什麼樣子:remote => true。rails 3 button_to:remote => true,控制器動作應該是什麼樣

我是新來的阿賈克斯reqs,所以我不知道我在做什麼。據我所知,the:remote => true將其設置爲ajax請求。

<%= button_to "Generate Code", add_code_band_path(@band), :method => :post, :remote => true %> 

我現在的行動:

def add_code 
     @band = Band.find(params[:id]) 
     if user_signed_in? 
     @code = current_user.codes.build(params[:code]) 

     respond_to do |format| 
     if @code.save 
      format.html { redirect_to @band, notice: 'Reward was successfully created.' } 
      format.json { render json: @band, status: :created, location: @band } 
      else 
      format.html { render action: "show" } 
      format.json { render json: @band.errors, status: :unprocessable_entity } 
      end 
     end 
     else 
     redirect_to @band, :alert => "Your Not Logged In! You must be logged in to create a code" 
     end 
    end 
+0

你有什麼錯誤? – 2012-01-18 03:59:02

+0

沒有錯誤,但屏幕刷新... – 2012-01-18 04:19:27

回答

8

與Ajax調用不能使用redirect_to的。你應該做這樣的事情來重定向(使用Ajax時):

render :js => "window.location = 'path_to_redirect_to'" 

如果問題仍然存在,嘗試包括format.js,在你的迴應來阻止。

+0

format.js {render:js =>「window.location ='path_to_redirect_to'」} – Naveed 2012-01-18 07:41:00

相關問題