레일스에서 양식에 약간 문제가 있습니다.레일 양식이 입력 값이있는 단일 매개 변수 해시를 생성하지 않습니다.
--- !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] 등을 사용하여 모든 값을 얻을 수는 있지만 모든 양식 입력 값을 갖는 단일 해시를 생성하지는 않습니다. 왜 이런 일이 일어나는 지 아는 사람이 있습니까? 미리 감사드립니다.
내가 틀릴 수도 있지만 'name : "name"'같은 것을 사용하여 레일의 기본 명명 구조를 무시하고 있다고 생각합니다. –
네 말이 맞아! bootstrapValidator가 유효성 검사를 위해 입력을받지 않기 때문에 필자는 그것들을 추가했다. 내가 그들을 제거하고 디버거가 표시됩니다 : 연락처 해시. 정말 고맙습니다! – Aurens