2012-01-18 7 views
0

となります。rails 3 button_to:remote => true、コントローラアクションの外観は

私は何をしているのかわからないので、私はajax reqsの新人です。私が理解しているように、: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

どのようなエラーが表示されますか? –

+0

エラーはありませんが、画面が更新されます。 –

答えて

0

をページには、あなたが言及したようちょうどさわやかであれば、あなたがいることにjQueryを使用している場合、あなたが含まれていると正しく(jQueryのための控えめなスクリプトアダプタを参照していることを確認である。)あなたはAjaxの呼び出しでredirect_toを使用することはできません

https://github.com/rails/jquery-ujs

8

。あなたは(AJAXを使用した場合)リダイレクトするために、このような何かを行う必要があります。

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

それでも問題が解決しない場合は、ブロックするようにあなたの応答では、format.js含めてみてください。

+0

format.js {render:js => "window.location = 'path_to_redirect_to'"} – Naveed

関連する問題