rails 3 button_to:remote => true,控制器动作应该是什么样子

TJ *_*ill 0 ruby-on-rails button

对于一个button_to,控制器动作应该是什么样的:remote => true.

我是ajax reqs的新手所以我不知道我在做什么.据我所知,:remote => true将其设置为ajax请求.

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

我目前的行动:

  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
Run Code Online (Sandbox Code Playgroud)

Wah*_*Ali 8

您不能将redirect_to与Ajax调用一起使用.你应该做这样的事情来重定向(当使用ajax时):

render :js => "window.location = 'path_to_redirect_to'"
Run Code Online (Sandbox Code Playgroud)

如果问题仍然存在,请在回复阻止时尝试包含format.js.