使用AJAX将Javascript变量传递给Controller

时间:2013-03-06 16:47:59

标签: ruby-on-rails ajax

我无法使用AJAX调用的data参数将变量传递给我的控制器。我做错了什么?

show.html.erb

var parentStepID = 20;

$.ajax({
    url: "/steps/create_branch",
    type: 'GET',
    data: {parent: parentStepID}
});

控制器

  def create_branch
    parentStepID = params[:parent]
    logger.debug("parentStepID: #{parentStepID}")
    respond_to do |format|
      format.js
    end
  end

记录器似乎没有得到parentStepID:

Started GET "/projects/20/steps/create_branch" for 127.0.0.1 at 2013-03-06 11:56:44 -0500
Processing by StepsController#create_branch as HTML
  Parameters: {"project_id"=>"20"}
  Project Load (0.1ms)  SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT 1  [["id", "20"]]
####################################################################
parentStepID: 
  Rendered steps/create_branch.js.erb (0.1ms)
Completed 200 OK in 16ms (Views: 10.6ms | ActiveRecord: 0.1ms)

浏览器控制台:

20 0:412
/projects/20/steps/create_branch 0:413

另外,我相信AJAX请求正常;我在我的步骤视图文件夹中创建了一个名为“create_branch.js.erb”的文件,并输入一个成功调用的警报。

0 个答案:

没有答案