我的网站上需要一个按钮,可以将信息发送到 Controller 的创建操作(“pagetimes”)。它似乎有效,尽管它没有发送我指定的所有数据——可能与我无法构建数据向量有关。我已通过 post 'pagetimes/create'
在 application.js 中:
function submitForm() {
alert("checked the button - worked");
$.ajax({
type:'POST',
url: '/pagetimes/create',
data: { pagename: "whatever", start: 7, end: 21 } ,
});
}
其中 :pagename
、:start
和 :end
是我的数据表中的列(字符串、整数、整数),并且可以访问在模型中,并且可在新页面时间的手动输入"new"页面中显示的内容中使用。
在我的页面 View 中:
<button type="button" onclick="submitForm()">Send data</button>
其他一切都非常标准。我可以在我的数据库中看到该帖子已成功提交,但我尝试填充的 3 个数据字段均为 NULL。这可能与我构建 data: {}
字段的方式有关?
O/w,Win7 上的 Rails 3,不使用任何可能对此有影响的其他花哨的东西......
更新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>
更新 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)
更新 3: Controller 操作
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
最佳答案
function submitForm() {
alert("checked the button - worked");
$.ajax({
type:'POST',
url: '/pagetimes/create',
data: $.param({ pagetime: {pagename: "whatever", start: 7, end: 21 }})
});
}
关于javascript - Ruby Rails - 为 AJAX 调用 Controller 操作构建数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20888250/