Ruby on Rails 3.0.9를 사용하고 있으며 "문의하기"양식을 직접 구현하려고합니다. 그래서 ... "문의하기"양식을 구현하기 위해 ActiveModel 사용시 문제가 발생했습니다
가 ... 내 모델 파일에 내가 가진 :require 'active_model'
class ContactUs
include ActiveModel::Conversion
include ActiveModel::Validations
attr_accessor :email, :subject, :message
def initialize(attributes = {})
@attributes = attributes
end
validates :email,
:presence => true
validates :subject,
:presence => true
validates :message,
:presence => true
def persist
@persisted = true
end
def persisted?
false
end
end
을 ... 내보기 파일에 내가 가진 :
<%= form_for @contact_us, :url => contact_us_path do |f| %>
<%= f.text_field :email %>
<%= f.text_field :subject %>
<%= f.text_area :message %>
<% end %>
을 ... 내 라우터 파일 I에 이 :
match 'contact_us' => 'pages#contact_us', :via => [:get, :post]
을 ... 내 컨트롤러 파일에서 내가 가진 :
class PagesController < ApplicationController
def contact_us
case request.request_method
when 'GET'
@contact_us = ContactUs.new
when 'POST'
@contact_us = ContactUs.new(params[:contact_us])
end
end
end
위의 코드를 사용하여 적어도 빈 필드가있는 양식을 제출할 때 (유효성 검사를 통과하지 못하도록하기 위해 양식을 제출할 때) 양식을 다시로드하면 해당 필드가 자동 채워지지 않습니다. . 즉, 양식을 다시로드 한 후 (제출 버튼을 누른 후에 발생) 필드 값이 모두 공백 값으로 설정됩니다.
무엇이 문제인가? ActiveModel
을 잘못 사용 했습니까?
ActiveRecord를 확장하는 대신에 'active_model'이 필요한 이유가 있습니까? 나는 Rails에 익숙하지 않고 아직 그것을 보지 못했다. – Nic
@melee 여기에서 있습니다 : https://github.com/rails/rails/blob/master/activemodel/examples/validations.rb – Backo