2014-11-21 7 views
1

레일스에서 ​​양식에 약간 문제가 있습니다.레일 양식이 입력 값이있는 단일 매개 변수 해시를 생성하지 않습니다.

--- !ruby/hash:ActionController::Parameters 
utf8: "✓" 
name: Jim 
email: [email protected] 
subject: hello 
message: goodbye 
controller: contacts 
action: create 

그것은 다음과 같이해야한다 :

은 내가 양식을 제출 한 후 얻을 것이 이것이다 (I 레일에 새로 온)

contact: 
    name: Jim 
    email: [email protected] 
    subject: hello 
    message: goodbye 

난 아무 생각 해요 무슨이 여기에서 잘못하고있다.

보기/연락처/new.html.erb

<%= form_for(@contact, url: contact_path) do |f| %> 
    <%= f.text_field :name, name: "name", value: nil, class: 'form-control', placeholder: 'Enter full name' %> 
    <%= f.email_field :email, name: "email", class: 'form-control', placeholder: 'Enter email address' %> 
    <%= f.text_field :subject, name: "subject", class: 'form-control', 
     placeholder: 'Enter subject' %> 
    <%= f.text_area :message, name:"message", class: 'form-control', rows: 6, placeholder: 'Enter your message for us here.' %> 
    <%= f.submit :submit, class: 'btn btn-default pull-right' %> 
<% end %> 

설정/routes.rb

get 'contact' => 'contacts#new' 
    post 'contact' => 'contacts#create' 

컨트롤러/contacts_controller : 여기 폼 (마이너스 모든 부트 스트랩 된 div 및 스팬)이다. RB

class ContactsController < ApplicationController 
    def new 
    @contact = Contact.new 
    end 

    def create 
    @contact = Contact.new(params[:contact]) #<-- always fails because no :contact 
    if @contact.valid? 
     if @contact.send_mail 
     # todo 
     else 
     # todo 
     end 
    else 
     flash.now[:error] = params.inspect 
     render 'new' 
    end 
    end 
end 

모델/contact.rb

class Contact 
    include ActiveAttr::Model 

    attribute :name 
    attribute :email 
    attribute :subject 
    attribute :message 

    validates_presence_of :name 
    validates_format_of :email, with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i 
    validates_presence_of :subject 
    validates_presence_of :message 

    def send_mail 
    ContactMailer.form_message(self).deliver_now 
    end 

end 

나는 form_for (: contact)를 사용하여 리소스로 라우팅하고 model을 mail_form gem을 사용하도록 변경했지만 여전히 운이 없다. 물론 params [: name] 등을 사용하여 모든 값을 얻을 수는 있지만 모든 양식 입력 값을 갖는 단일 해시를 생성하지는 않습니다. 왜 이런 일이 일어나는 지 아는 사람이 있습니까? 미리 감사드립니다.

+0

내가 틀릴 수도 있지만 'name : "name"'같은 것을 사용하여 레일의 기본 명명 구조를 무시하고 있다고 생각합니다. –

+0

네 말이 맞아! bootstrapValidator가 유효성 검사를 위해 입력을받지 않기 때문에 필자는 그것들을 추가했다. 내가 그들을 제거하고 디버거가 표시됩니다 : 연락처 해시. 정말 고맙습니다! – Aurens

답변

0

도우미가 이미 이름을 설정 했으므로 설정 한 이름이 아니기 때문에 name: '...' 옵션을 제거하십시오. 귀하의 경우, 레일은 필드 이름이 contact[name]contact[email]과 같다고 예상합니다.

+0

위선자, 나는 @japed가 주석을다는 것을 보지 못했다. 그럼 내 대답을 무시하십시오. – rewritten

+0

고마워 :-) – Aurens