2012-07-13 93 views
0

我在rails代碼上寫了ruby。我使用ajax請求將數據從客戶端發送到服務器。但問題是它不會將該數據保存到數據庫中。由於我對rails的ruby是全新的,所以我不知道它爲什麼不起作用。我還沒有得到任何錯誤用戶數據不保存到數據庫中

這裏是我的代碼

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表單。我使用haml代替計劃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'} 

回答

2

它看起來像您的HTML輸入名稱和您的控制器不能一起工作。通常,在Rails中,當您使用the built-in form helpers,所有的字段名的命名空間與您的模型:

<input name="contact[name]"> 

這是什麼讓你做這樣的事情在你的控制器Contact.new(params[:contact])。在你的情況下,我不能告訴表單發生了什麼,因爲你沒有發佈它,但我假設根據你如何訪問控制器中的params變量,這是問題。我會建議使用表單助手,以便您的HTML遵循名稱空間參數的預期慣例。然後,你就可以到你的控制器更改爲:

@contacts = Contact.new(params[:contact]) 

if @contacts.save 
    redirect_to root_path, :notice => "your post is saved" 
else 
    render "new" 
end 

有一點需要注意的是,盲目使用PARAMS實例化對象可能會導致安全漏洞。確保你閱讀security guide section on mass-assignment以更好地理解這一點。

+0

請參閱我的更新。我添加了html表單。 – 2619 2012-07-13 10:59:00

+0

@ al0neevenings他指出,你的形式是不正確的。不要用':name =>'name''傳遞一個表單,而應該通過':name =>'contact [name]'',然後對所有表單的輸入進行重複。 – Draiken 2012-07-13 11:12:55