나는 그것을 작동 시키려고 노력하고 있지만 그것을하지는 않습니다!레일 3, 중첩 된 다중 레벨 양식 및 has_many부터
나는
class User < ActiveRecord::Base
has_many :events, :through => :event_users
has_many :event_users
accepts_nested_attributes_for :event_users
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :event_users
accepts_nested_attributes_for :users
end
class EventUser < ActiveRecord::Base
set_table_name :events_users
belongs_to :event
belongs_to :user
accepts_nested_attributes_for :events
accepts_nested_attributes_for :users
end
또한 테이블 레이아웃
event_users
user_id
event_id
user_type
events
id
name
users
id
name
을 그리고 이것은 내 양식이다
<%= semantic_form_for @event do |f| %>
<%= f.semantic_fields_for :users, f.object.users do |f1| %>
<%= f1.text_field :name, "Name" %>
<%= f1.semantic_fields_for :event_users do |f2| %>
<%= f2.hidden_field :user_type, :value => 'participating' %>
<% end %>
<% end %>
<%= link_to_add_association 'add task', f, :users %>
<% end %>
문제는 그 나는 새 사용자에게이 방법을 만드는 경우 , user_type의 값을 설정하지 않습니다 (그러나 user_id 및 event_id를 사용하여 사용자 및 event_users를 작성합니다). 사용자 생성 후 제출 후 편집 양식으로 돌아 가면 user_type의 값이 events_users에 설정됩니다. (나는 또한 formtastic없이 시도했습니다) 어떤 제안? 감사!
---- 편집 ---- 또한 사용자
<%= semantic_form_for @event do |f| %>
<%= f.semantic_fields_for :event_users do |f1| %>
<%= f1.hidden_field :user_type, :value => 'participating' %>
<%= f1.semantic_fields_for :users do |f2| %>
<%= f2.text_field :name, "Name" %>
<% end %>
<% end %>
<%= link_to_add_association 'add task', f, :event_users %>
<% end %>
을하기 전에 event_users을 가지고 시도했지만 그것은 단지 나에게 오류가 발생
:
User(#2366531740) expected, got ActiveSupport::HashWithIndifferentAccess(#2164210940)
을 --edit--
link_to_association은 formtastic-cocoon 방식입니다 (https://github.com/nathanvda/formtast). IC-누에 고치가)하지만 난
def create
@event = Event.new(params[:event])
respond_to do |format|
if @event.save
format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
format.xml { render :xml => @event, :status => :created, :location => @event }
else
format.html { render :action => "new" }
format.xml { render :xml => @event.errors, :status => :unprocessable_entity }
end
end
end
컨트롤러 생성 작업을 표시 할 수 있습니까? – Nerian
이제 추가되었습니다! – jonepatr