nested_form gem을 사용하여 체인 및 그 위치의 목록을 보여주는 드롭 다운을 만들려고합니다. 사용자가 제출 버튼을 클릭하면 nested_form은 users_locations라는 조이너 테이블에 새 항목을 만들어야합니다. 그러나 다음과 같이 형태가 오류를 뱉어 : 즉레일 : nested_form_for <column>은 비워 둘 수 없습니다.
User locations user can't be blank
을, 그는 USER_ID가 NULL 불평하고 USER_ID가 user_locations 테이블에 필수이기 때문에 따라서는 (테이블에 항목을 만들 수 없습니다).
belongs_to :chain
has_many :user_locations
has_many :locations, :through => :user_locations
...
accepts_nested_attributes_for :user_locations, :reject_if => :all_blank, :allow_destroy => true
validates_associated :user_locations, :message => ": you have duplicate entries."
...
attr_accessible :first_name, :last_name, ... , :user_locations_attributes
users_controller.rb
def create
@user = User.new(params[:user])
@user.save ? (redirect_to users_path, notice: 'User was successfully created.') : (render action: "new")
end
로그인
<div class="field" id="location_toggle">
<%= f.label "Location:", :class => "form_labels", required: true %>
<%= f.fields_for :user_locations do |location| %>
<%= location.grouped_collection_select :location_id, Chain.all, :locations, :name, :id, :name, include_blank: false %>
<% end %>
<%= f.link_to_add "Add a Location", :user_locations, :class => "btn btn-primary btn-small" %>
<br/>
</div>
user.rb (모델) (사용자보기 폴더에서)
_form.html.erb 보여줍니다 :
SELECT 1 AS one FROM "user_locations" WHERE ("user_locations"."user_id" IS NULL AND "user_locations"."location_id" = 1)
@ 사용자)'블록? – charleyc
예! 그것은 <% = simple_nested_form_for (@user) 안에 존재합니다. do | f | %> – Rahul