2

私はそれを機能させようとしていますが、そうではありません!

私は持っています

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
4

2 に答える 2

2

正直なところ、私はそのように編集したり作成したりしたことはありませんhas_many :through

少し時間がかかり、formtastic_cocoon内のjsを修正して機能させる必要があったため、これが機能するソリューションです。

EventUserモデルを指定してから、モデルを埋める必要がありますUser(逆の場合は機能しません)。

したがって、モデルの内部では次のように記述します。

class Event < ActiveRecord::Base
  has_many :event_users
  has_many :users, :through => :event_users
  accepts_nested_attributes_for :users, :reject_if => proc {|attributes| attributes[:name].blank? }, :allow_destroy => true
  accepts_nested_attributes_for :event_users, :reject_if => proc {|attributes| attributes[:user_attributes][:name].blank? }, :allow_destroy => true
end

class EventUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :event
  accepts_nested_attributes_for :user
end

class User < ActiveRecord::Base
  has_many :events, :through => :event_users
  has_many :event_users
end

次に、ビュー。から始めますevents/_form.html.haml

= semantic_form_for @event do |f|
  - f.inputs do
    = f.input :name

    %h3 Users (with user-type)
    #users_with_usertype
      = f.semantic_fields_for :event_users do |event_user|
        = render 'event_user_fields', :f => event_user
      .links
        = link_to_add_association 'add user with usertype', f, :event_users

    .actions
      = f.submit 'Save'

(今のところエラーは無視します)次に、部分的な_event_user_fields.html.haml部分を指定する必要があります(ここに少し魔法がかかります):

.nested-fields
  = f.inputs do
    = f.input :user_type, :as => :hidden, :value => 'participating'
    - if f.object.new_record?
      - f.object.build_user
    = f.fields_for(:user, f.object.user, :child_index => "new_user") do |builder|
      = render("user_fields", :f => builder, :dynamic => true)

パーシャルを終了します_user_fields(実際にはパーシャルである必要はありません)

.nested-fields
  = f.inputs do
    = f.input :name

これは機能するはずです。formtastic_cocoon gemを更新する必要があったため、バージョン0.0.2に更新する必要があることに注意してください。

user_typeこれで、非表示フィールドの代わりに、単純なドロップダウンからを選択することが簡単に可能になります。

= f.input :user_type, :as => :select, :collection => ["Participator", "Organizer", "Sponsor"]

いくつかの考え(今私はそれが機能することを証明しました):

  • これにより、常にその場で新しいユーザーが作成され、実際にはの必要がなくなりEventUserます。ドロップダウンから既存のユーザーを選択することもできますか?
  • 個人的に私はそれを好転させます:ユーザーに自分自身をイベントに割り当てさせてください!
于 2011-01-19T22:20:20.263 に答える
0

events_usersモデルにID列がありませんか?追加のフィールド(user_type)があるため、EventUserはモデルであり、おそらくIDが必要です。たぶんそれが、最初のケースでuser_typeが設定されていない理由です。

于 2011-01-14T18:38:23.240 に答える