2012-07-13 6 views
0

레일즈 코드에 루비를 쓰고 있습니다. 아약스 요청을 사용하여 클라이언트에서 서버로 데이터를 보내고 있습니다. 그러나 문제는 데이터를 데이터베이스에 저장하지 않는다는 것입니다. 루비에 대한 새로운 지식이 생겨서 왜 작동하지 않는지 전혀 알지 못합니다. 또한사용자 데이터가 데이터베이스에 저장되지 않습니다

여기
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'} 

답변

2

HTML 입력 이름처럼 보이지만 컨트롤러가 함께 작동하지 않습니다. 당신이 the built-in form helpers를 사용할 때 일반적으로 레일에, 모든 필드 이름은 모델에 네임 스페이스된다

<input name="contact[name]"> 

이것은 당신이 당신의 컨트롤러 Contact.new(params[:contact]) 같은 일을 무엇을 할 수 있습니다. 귀하의 경우에는 그것을 게시하지 않았기 때문에 양식에 어떤 일이 일어나는지 말할 수는 없지만 컨트롤러의 params 변수에 어떻게 접근하고 있는지에 따라 가정하고 있습니다. 이것이 문제입니다. HTML이 네임 스페이스 매개 변수의 예상되는 규칙을 따르도록 양식 도우미를 사용하는 것이 좋습니다. 맹목적으로 PARAMS를 사용하여 인스턴스 객체가 보안 구멍으로 이어질 수 있다는 것입니다 조심

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

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

한 가지 : 그런 다음에 컨트롤러를 변경할 수 있습니다. 이 점을 더 잘 이해하려면 security guide section on mass-assignment을 꼭 읽으십시오.

+0

내 업데이트를 참조하십시오. html 양식을 추가했습니다. – 2619

+0

@ al0neevenings 그가 지적한대로 양식이 잘못되었습니다. ': name => 'name'과 함께 폼을 전달하는 대신': name => 'contact [name]''을 전달해야하며 모든 폼의 입력에 대해 이것을 반복하십시오. – Draiken

관련 문제