레일 3.2를 사용 중이며 중첩 된 양식을 작성하고 있습니다. 하지만 내가 예상 한대로 작동하지 않습니다. 우선, 제 모델은 주소가 많은 회사입니다. 당신이 회사의 모델이 볼 수 있듯이 여기에 모델mongoid 및 validates_associated가 작동하지 않습니다.
class Company
include Mongoid::Document
include Mongoid::Timestamps
field :name, :type => String
field :description, :type => String
field :order_minimun, :type => Float
belongs_to :user
has_many :addresses
validates_presence_of :name, :description, :order_minimun
validates_length_of :name, minimum:2, maximum: 30
validates_length_of :description, minimum:5, maximum: 140
validates :order_minimun, :numericality => { :only_integer => true, :greater_than_or_equal_to => 0 }
accepts_nested_attributes_for :addresses
validates_associated :addresses
end
class Address
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Spacial::Document
field :street, :type => String
field :number, :type => Integer
field :phone, :type => String
field :location, :type => Array, spacial: {lat: :latitude, lng: :longitude, return_array: true }
embeds_many :delivery_zones
belongs_to :company
belongs_to :city
has_many :openingTimeRange
validates_presence_of :street, :number
validates_length_of :street, minimum:1, maximum: 30
validates_length_of :number, minimum:1, maximum: 6
validates_length_of :phone, minimum:5, maximum: 60
attr_accessible :street, :number, :company_id, :city_id, :location, :phone, :delivery_zones, :latitude, :longitude
end
입니다 : 그래서
accepts_nested_attributes_for :addresses
validates_associated :addresses
, 나는이 중첩 된 형태를 만들 수 있다고 생각합니다. 여기 형태
<%= form_for [:admin,@company],:url =>admin_company_path(@company), :html => {:class => "form-horizontal"} do |f|%>
<legend><%= t '.legend' %></legend>
<%= group_input_field f, :name%>
<%= group_field_for f, :description do%>
<%= f.text_area :description, :rows => 5%>
<% end -%>
<%= group_input_field f, :order_minimun%>
<%= f.fields_for :addresses do |builder|%>
<%= render 'address_fields', :f=> builder%>
<% end %>
<div class="form-actions">
<%= f.submit :class => 'btn btn-primary btn-large', :disable_with => t('button.saving') %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
admin_companies_path, :class => 'btn btn-large btn-danger' %>
</div>
<% end %>
_address_fields.html.erb의 코드는
<%= group_input_field f, :street%>
<%= group_input_field f, :number%>
내가 부트 스트랩
def group_input_field(f,field, options={})
has_error = f.object.errors.has_key? field
klass = has_error ? "control-group error": "control-group"
content_tag(:div, :class => klass) do
f.label(field, :class => 'control-label')+
content_tag(:div, :class => 'controls') do
f.text_field(field, :class => 'input')+
show_error(f.object,field,has_error)+
show_help(options)
end
end
end
마침내 컨트롤러와 양식 필드를 생성하는 간단한 도우미가 :
class Admin::CompaniesController < ApplicationController
def new
#crea una nueva compañia
@company = Company.new
end
def edit
@company = Company.find params[:id]
end
def create
@company = Company.new(params[:company])
if @company.save
redirect_to :action => 'index'
else
render 'new'
end
end
def update
@company = Company.find(params[:id])
if @company.update_attributes(params[:company])
redirect_to :action => 'index'
else
render 'edit'
end
end
end
몇 가지 일이 일어나고 있습니다. 첫째, 두 개의 주소가있는 회사가 있으며 첫 번째 주소를 편집 할 수 있습니다. 두 번째 주소의 변경 사항은 유지되지 않습니다. 그런 다음 주소 필드의 유효성이 검사되지 않습니다 (양식을 다시 열 때 모든 항목을 비워두면 주소가 저장되지 않고 원래 값을 볼 수 있음). 그리고 어떤 주소의 필드를 편집 할 때 회사의 필드 값이 유효하지 않은 경우 양식을 제출 한 후에 회사 모델의 오류를 볼 수 있지만 주소는 원래 값으로 표시되므로 편집 된 값은 잃어버린.
희망을 명확히하십시오.
미리 감사드립니다.