问题描述
我正在Rails代码上编写ruby。我正在使用ajax请求将数据从客户端发送到服务器。但是问题在于它不会将数据保存到数据库中。由于我完全不熟悉红宝石,所以我不知道为什么它不起作用。我也没有得到任何错误
I am writing ruby on rails code. I am sending data from client to server using ajax request. But the problem is that it does not save that data to database. Since I am completely new to ruby on rails so I have no idea why it is not working. I also do not get any errors
这是我的代码
class ContactsController < ApplicationController
def contacts
name = params["name"]
company = params["company"]
email = params["email"]
phone = params["phone"]
@contacts = Contact.new(params[:post])
if @contacts.save
redirect_to root_path, :notice => "your post is saved"
else
render "new"
end
end
end
这是我的js代码
$('.signupbutton').click(function(e) {
e.preventDefault();
var data = $('#updatesBig').serialize();
var url = 'contacts';
console.log(data);
$.ajax({
type: 'POST',
url: url,
data: data,
success: function(data) {
console.log('done');
}
});
});
这是我在控制台中的输出
and here is my output in console
Started POST "/contacts" for 127.0.0.1 at 2012-07-13 13:25:41 +0300
Processing by ContactsController#contacts as */*
Parameters: {"name"=>"asdsa", "company"=>"asdsa", "email"=>"asdasd", "phone"=>"asdasd"}
(0.1ms) begin transaction
SQL (18.1ms) INSERT INTO "contacts" ("company", "created_at", "email", "group", "name", "phone", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["company", nil], ["created_at", Fri, 13 Jul 2012 10:25:41 UTC +00:00], ["email", nil], ["group", nil], ["name", nil], ["phone", nil], ["updated_at", Fri, 13 Jul 2012 10:25:41 UTC +00:00]]
(3.9ms) commit transaction
Redirected to http://0.0.0.0:3000/
Completed 302 Found in 33ms (ActiveRecord: 22.5ms)
更新
这是我的html表单。我正在使用haml而不是计划html.erb
Here is my html form. I am using haml instead of plan html.erb
%form#updatesBig{:target => 'contacts', :method => 'post'}
%div
%label{:for => 'form_updatesBig_name'} Name
%input{:type => "text", :id => 'form_updatesBig_name', :name => 'name', :class => 'text name required'}
%label{:for => 'form_updatesBig_company'} Organization
%input{:type => "text", :id => 'form_updatesBig_company', :name => 'company', :class => 'text company required'}
%div
%label{:for => 'form_updatesBig_email'} E-mail
%input{:type => "text", :id => 'form_updatesBig_email', :name => 'email', :class => 'text email required'}
%label{:for => 'form_updatesBig_phone'} Phone
%input{:type => "text", :id => 'form_updatesBig_phone', :name => 'phone', :class => 'text phone required'}
%div
%input.button.signupbutton{:type => 'submit', :value => 'Sign up'}
推荐答案
看起来像您的HTML输入名称和您的控制器无法一起工作。通常在Rails中,当您使用时,所有字段名称都使用以下名称空间命名您的模型:
It looks like your HTML input names and your controller are not working together. Normally in Rails, when you use the built-in form helpers, all the field names are namespaced with your model:
<input name="contact[name]">
这是允许您执行 Contact.new(params [:联系人])
。在您的情况下,由于您没有发布表单,所以我无法判断表单的状态,但是我假设是基于您访问控制器中params变量的方式。我建议您使用表单助手,以便您的HTML遵循命名空间参数的预期约定。然后,您可以将控制器更改为:
This is what allows you do things like Contact.new(params[:contact])
in your controller. In your case I can't tell what is going on with the form since you didn't post it, but I'm assuming based on how you are accessing the params variables in your controller, this is the issue. I would recommend using the form helpers so your HTML follows the expected convention of namespaced params. Then you'll be able to change your controller to:
@contacts = Contact.new(params[:contact])
if @contacts.save
redirect_to root_path, :notice => "your post is saved"
else
render "new"
end
需要注意的一件事是,使用参数盲目实例化对象可能会导致安全漏洞。请确保您已阅读,以更好地理解这一点。
One thing to watch out for is that instantiating objects blindly using params can lead to security holes. Make sure you read the security guide section on mass-assignment to understand this better.
这篇关于用户数据未保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!