2
까지

나는 그것을 작동 시키려고 노력하고 있지만 그것을하지는 않습니다!레일 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 
+0

컨트롤러 생성 작업을 표시 할 수 있습니까? – Nerian

+0

이제 추가되었습니다! – jonepatr

답변

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에 대한 필요성을 제거, 즉석에서 새로운 사용자를 생성합니다

  • . 드롭 다운에서 기존 사용자를 선택할 수도 있습니까?
  • 개인적으로 나는 주변을 돌릴 것입니다 : 사용자가 이벤트에 자신을 할당 할 수있게하십시오!
+0

와우! 나는 당신이 이것을 실제로 풀었다는 것을 믿을 수 없다! 너는 나의 영웅이야! 정말 고맙습니다!! 주최자가 모든 참가자를 추가하는 이유는 이벤트 중에 발생하는 경제적 관련 문제를 해결하기 위해 실제 이벤트 이후에 이벤트가 추가된다는 것입니다. 예, 기존 사용자를 추가하고 있습니다! 다시 한 번 감사드립니다! – jonepatr

+0

멀티 드롭 다운에서 선택된 사용자 (기존)에게 user_type을 추가하는 방법에 대해 알고 있습니까? – jonepatr

+0

'_event_user_fields' 부분에서는 자동으로 새로운 사용자를 만듭니다. 이 작업을 수행하는 대신 새 사용자를 추가하려면 1을, 드롭 다운에서 기존 사용자를 추가하려면 1을 두 가지 링크로 사용할 수 있습니다. 그게 충분히 명확한가요? – nathanvda

0

는 events_users 모델은 ID 열이 없음 ---- 다른 접근을 할 수 있지만 같은 결과

--- 편집과 함께하려고 노력 해요? 추가 필드 (user_type)가 있으므로 EventUser는 모델이며 ID가 있어야합니다. 아마도 이것이 첫 번째 경우에 user_type이 설정되지 않은 이유 일 수 있습니다.

+0

ID를 추가하려고 시도했지만 아무런 차이가 없습니다./ – jonepatr

+0

이상한 점은 업데이트시에 beeing이 설정된다는 것입니다. – jonepatr