나는 rails.I의 새내기입니다 : Form과 FormType.A form_type은 0 또는 하나의 form을 가질 수 있습니다. 하나의 form은 하나의 form_type에 속합니다. 아래와 같이 모델 :simple_form_for @form에서 f.association의 유효성 확인
class FormType < ActiveRecord::Base
has_one :form
attr_accessible :name
end
class Form < ActiveRecord::Base
belongs_to :form_type
validates :name, :presence => true
validates_presence_of :form_type
attr_accessible :name, :enabled
end
내 _form.html.erb이
<%= simple_form_for(@form, :html => { :class => 'form-horizontal' },
:url => @form.new_record?() ? admin_forms_path : admin_form_path,
:method => @form.new_record?() ? 'post':'put') do |f| %>
<div class="form-inputs">
<%= f.input :name, :required => true, :autofocus => true %>
<%= f.association :form_type,:required => true, :hint => "select type" %>
<%= f.input :enabled %>
</div>
<div class="form-actions">
<%= f.button :submit, :class => 'btn-primary' %>
<% if ! @form.new_record?() %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
admin_form_path(@form),
:method => 'delete',
:confirm => t('.confirm', :default => t("helpers.links.confirm")),
:class => 'btn btn-danger' %>
<% end %>
</div>
<% end %>
이 내 컨트롤러 코드는 다음과 같습니다.
def create
@form = Form.new
@form.name = params[:form][:name]
@form.enabled = params[:form][:enabled]
@form.form_type_id = params[:form][:form_type_id].to_i
if @form.save
redirect_to :action => 'index'
else
render action: "new"
end
끝
나는이 문제를 해결하기 위해 내 실수 또는 유용한 기사를 가리 킵니다 사람 association.Can의 유효성을 검사 할 수 서버 측 valdiation 이름 필드에 발사 볼 수 있지만입니다.
컨트롤러 코드를 게시해야하는 이유는 : url 및 : method를 simple_form_for 메서드에 전달하는 이유와 같은 질문이 너무 많기 때문입니까? 이 작업을 수행 할 필요가 없으며 @business_form 또는 @form이라는 객체 인스턴스도 있어야합니다. 당신은 모두 html로 – ADAM
@ 아담 나는 컨트롤러 코드 now.my 개체를 가지고있다 business_form – Ramoji