2012-01-18 75 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

相关问题