私はそれを機能させようとしていますが、そうではありません!
私は持っています
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を使用してuserとevent_usersが作成されます)。ユーザーの作成後に編集フォームに戻って送信すると、user_typeの値がevents_usersに設定されます。(私もformtasticなしで試しました)何か提案はありますか?ありがとう!
- - 編集 - -
また、ユーザーの前にevent_usersを設定しようとしました
<%= 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 %>
しかし、それは私にエラーを投げるだけです:
ユーザー(#2366531740)が期待され、ActiveSupport :: HashWithIndifferentAccess(#2164210940)を取得しました
- 編集 -
link_to_associationはformtastic-cocoonメソッド(https://github.com/nathanvda/formtastic-cocoon)ですが、他のアプローチを試みましたが、同じ結果になりました
- -編集 - -
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