Reputation: 32941
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
Here is my code
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
Here is my js code
$('.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)
Update
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'}
Upvotes: 0
Views: 2731
Reputation: 51717
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]">
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.
Upvotes: 2