레일즈 코드에 루비를 쓰고 있습니다. 아약스 요청을 사용하여 클라이언트에서 서버로 데이터를 보내고 있습니다. 그러나 문제는 데이터를 데이터베이스에 저장하지 않는다는 것입니다. 루비에 대한 새로운 지식이 생겨서 왜 작동하지 않는지 전혀 알지 못합니다. 또한사용자 데이터가 데이터베이스에 저장되지 않습니다
여기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');
}
});
});
이며, 여기에 콘솔
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 양식입니다. html.erb 대신 haml을 사용합니다.
%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 양식을 추가했습니다. – 2619
@ al0neevenings 그가 지적한대로 양식이 잘못되었습니다. ': name => 'name'과 함께 폼을 전달하는 대신': name => 'contact [name]''을 전달해야하며 모든 폼의 입력에 대해 이것을 반복하십시오. – Draiken