javascript Ruby Rails - 为控制器动作的 AJAX 调用构建数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20888250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby Rails - structuring data for AJAX call to controller action
提问by SOConnell
I have need for a button on my site which can send information to the create action of a controller ("pagetimes"). It seems to be working, although it is not sending all the data I am specifying--probably having to do with my inability to structure the data vector. I have made POST requests available in my config/routes.rb file via post 'pagetimes/create'
我的网站上需要一个按钮,可以将信息发送到控制器的创建操作(“pagetimes”)。它似乎正在工作,尽管它没有发送我指定的所有数据——可能与我无法构建数据向量有关。我已经在我的 config/routes.rb 文件中通过了 POST 请求post 'pagetimes/create'
In application.js:
在 application.js 中:
function submitForm() {
alert("checked the button - worked");
$.ajax({
type:'POST',
url: '/pagetimes/create',
data: { pagename: "whatever", start: 7, end: 21 } ,
});
}
Where :pagename
, :start
and :end
are columns in my data table (string, integer, integer) and are made accessible in the model, and are available in what shows in the manual entry "new" page for a new pagetime.
其中:pagename
,:start
和:end
是我的数据表中的列(字符串、整数、整数)并且可以在模型中访问,并且可以在新页面时间的手动条目“新”页面中显示的内容中使用。
In my page view:
在我的页面视图中:
<button type="button" onclick="submitForm()">Send data</button>
Everything else is pretty standard. I can see in my database that the post is being submitted successfully, but the 3 data fields I am trying to populate are all NULL. Probably this has something to do with how I am structuring the data: {}
field?
其他一切都很标准。我可以在我的数据库中看到帖子已成功提交,但我尝试填充的 3 个数据字段都是 NULL。可能这与我如何构建该data: {}
领域有关?
O/w, Rails 3 on Win7, not using anything else fancy that might have a bearing on this...
O/w,Win7 上的 Rails 3,没有使用任何其他可能与此有关的花哨的东西......
UPDATE 1: This is what the form source code looks like that I am trying to post into. Maybe it is the case that I am referring to the fields incorrectly?
更新 1:这就是我试图发布的表单源代码的样子。也许是我错误地引用了这些字段?
<div class="form-inputs">
<div class="control-group string optional pagetime_pagename"><label class="string optional control-label" for="pagetime_pagename">Pagename</label><div class="controls"><input class="string optional" id="pagetime_pagename" name="pagetime[pagename]" size="50" type="text" /></div></div>
<div class="control-group integer optional pagetime_start"><label class="integer optional control-label" for="pagetime_start">Start</label><div class="controls"><input class="numeric integer optional" id="pagetime_start" name="pagetime[start]" step="1" type="number" /></div></div>
<div class="control-group integer optional pagetime_end"><label class="integer optional control-label" for="pagetime_end">End</label><div class="controls"><input class="numeric integer optional" id="pagetime_end" name="pagetime[end]" step="1" type="number" /></div></div>
</div>
UPDATE 2: Here is the piece of my logs that contains the POST:
更新 2:这是我的包含 POST 的日志片段:
Started POST "/pagetimes/create" for 127.0.0.1 at 2014-01-02 12:45:48 -0500
Processing by PagetimesController#create as */*
Parameters: {"end"=>"21", "pagename"=>"whatever", "start"=>"7"}
[1m[35mUser Load (1.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[35mSQL (15.0ms)[0m INSERT INTO "pagetimes" ("created_at", "end", "pagename", "start", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Thu, 02 Jan 2014 17:45:48 UTC +00:00], ["end", nil], ["pagename", nil], ["start", nil], ["updated_at", Thu, 02 Jan 2014 17:45:48 UTC +00:00], ["user_id", 1]]
[1m[36m (7.0ms)[0m [1mcommit transaction[0m
Redirected to http://localhost:3000/pagetimes/22
Completed 302 Found in 91ms (ActiveRecord: 24.0ms)
UPDATE 3: Controller action
更新 3:控制器动作
def create
@pagetime = Pagetime.new(params[:pagetime])
@pagetime.user = current_user
respond_to do |format|
if @pagetime.save
format.html { redirect_to @pagetime, notice: 'Pagetime was successfully created.' }
format.json { render json: @pagetime, status: :created, location: @pagetime }
else
format.html { render action: "new" }
format.json { render json: @pagetime.errors, status: :unprocessable_entity }
end
end
end
采纳答案by arun15thmay
function submitForm() {
alert("checked the button - worked");
$.ajax({
type:'POST',
url: '/pagetimes/create',
data: $.param({ pagetime: {pagename: "whatever", start: 7, end: 21 }})
});
}
回答by HarsHarI
$.post( "/pagetimes/create", { pagename: "whatever", start: "7", end: "21" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});