1

저는 세 모델이 있습니다 : Event, WorkoutRound입니다.양식을 이중 중첩 양식으로 저장하려면 어떻게합니까?

개인은 운동을 포함하는 이벤트를 만들고 라운드를 통해 세트 및 가중치 수를 구성 할 수 있습니다.

현재 중첩 된 양식을 만들기 위해 cocoon 보석을 사용하고 있습니다. 양식을 사용하고 이벤트 및 운동을 저장할 수 있지만 라운드가 저장되지 않습니다.

class Event < ActiveRecord::Base 
    has_many :workouts, dependent: :destroy 
    has_many :rounds, :through => :workouts 
    belongs_to :user 
    accepts_nested_attributes_for :workouts, :allow_destroy => true 
end 

class Workout < ActiveRecord::Base 
    belongs_to :event 
    has_many :rounds, dependent: :destroy 
    accepts_nested_attributes_for :rounds, :allow_destroy => true 
end 

class Round < ActiveRecord::Base 
    belongs_to :workout 
    belongs_to :event 
end 

현재 내 경로는 다음과 같이 설정되어 있습니다.

Rails.application.routes.draw do 
    devise_for :users 
    root 'static_pages#index' 
    resources :events do 
    resources :workouts do 
     resources :rounds 
     end 
    end 

내 컨트롤러에서 이것은 새로운 방법을 사용하는 방법입니다.

New Method for Event 
    def new 
    @event = current_user.events.new 
    end 

    New Method for Workout 
    def new 
    @workout = Workout.new 
    end 

    New Method for Round 
    def new 
    @round = Round.new 
    end 

현재 이벤트보기 폴더 아래에 양식이 있습니다. 이벤트의 show.html.erb이 파일을 볼에서 나는

<% @workout.rounds.each do |round| %> 
    <%= round.weight %> 
<% end %> 

에 의해뿐만 아니라 라운드를 표시하려고하지만 undefined method for rounds을 얻고있다. 이벤트보기에서 라운드를 표시 할 수 있습니까?

도움 주셔서 감사합니다.


편집 1 :

여기 내 중첩 된 형태이다. 맨 위에는 이벤트 용 양식이 있습니다.

<%= simple_form_for @event do |f| %> 
    <%= f.input :title %> 

     <h3>Work Outs</h3> 
     <div id="workouts"> 
     <%= f.simple_fields_for :workouts do |workout| %> 
      <%= render 'workout_fields', f: workout %> 
     <% end %> 
     <div class="links"> 
      <%= link_to_add_association 'add workout', f, :workouts %> 
     </div> 
     </div> 

     <div class="field"> 
     <%= f.label :start_time %><br> 
     <%= f.datetime_select :start_time %> 
     </div> 
     <div class="field"> 
     <%= f.label :end_time %><br> 
     <%= f.datetime_select :end_time %> 
     </div> 
     <div class="field"> 
     <%= f.label :description %><br> 
     <%= f.text_area :description %> 
     </div> 
     <div class="actions"> 
     <%= f.submit "Create new Workout" %> 
     </div> 
    <% end %> 
<% end %> 

</form> 

는 그런 운동

<div class="nested-fields"> 
    <%= f.input :name %> 

    <div class="rounds"> 
    <%= f.simple_fields_for :rounds, :wrapper => 'inline' do |round| %> 
     <%= render 'round_fields', f: round %> 
    <% end %> 
    <div class="links"> 
     <%= link_to_add_association 'add round', f, :rounds, :render_option => { :wrapper => 'inline' } %> 
    </div> 
    </div> 
    <%= link_to_remove_association "remove workout", f %> 
</div> 

의 형태가 그리고 마지막으로, 나는 레일 콘솔에서 라운드 생성하고 저장할 수 있어요 라운드

<div class="nested-fields"> 

    <table class="table round-table"> 
    <tr> 
     <td> 
     <% index = 0 %> 
     <%= f.simple_fields_for :rounds do |round| %> 
      <%= render 'set_fields', {f: round, index: index} %> 
      <% index = index + 1 %> 
     <% end %> 
     </td> 
     <td>Previous Weight</td> 
     <td><%= f.input_field :weight %></td> 
     <td><%= f.input_field :repetition %></td> 
     <td><%= link_to_remove_association "remove rounds", f %></td> 
    </tr> 
    </table> 
</div> 

의 형태를 갖는다. 그러나 웹에서 양식을 사용하면 저장할 수 없습니다.


편집 2

이 내가 event_paramsworkout_params 셋업을 얼마나 현재. 그런 다음 workout_params

def event_params 
    params.fetch(:event, {}).permit(:title, :description, :start_time, :end_time, :workouts_attributes => [:id, :name, :category, :_destroy]) 
end 

는 :

def workout_params 
    params.require(:workout).permit(:name, :category, :rounds_attributes => [:id, :weight, :set, :repetition, :_destroy]) 
end 

나는 양식 이벤트와 운동을 절약 왜 혼란 스러워요. 그러나 Round는 항상 빈 배열을 반환합니다.

+0

'EventsController # show' 액션에서 @workout을 설정하고 있습니까? – user3680688

+0

has_many associations 때문에 필요하지 않다고 생각했습니다. 운동을 event.workouts에 설정해야합니까? –

답변

0

이벤트 모델 (has_many :rounds, :through => :workouts)에 rounds 속성이 이미 있습니다. 왜 사용하지 않으시겠습니까?

<% @event.rounds.each do |round| 
    <%= round.weight %> 
<% end %> 
+0

@event에서 직접 액세스 할 수 있다는 것을 알지 못했습니다. 고맙습니다. 라운드에서 숫자가 표시되지 않기 때문에 내 라운드 중 아무 것도 저장되지 않은 것처럼 보입니다. –

1

마지막으로 해결했습니다.

나는 params에도 연관이 있어야했습니다. 이중 중첩 연결입니다. 내 event_params를 들어

def event_params 
    params.fetch(:event, {}).permit(:title, :description, :start_time, :end_time, :workouts_attributes => [:id, :name, :category, :_destroy]) 
end 

, 나는 단지 :workouts_attributes했다. 나는 을 workout_params에 넣는 것이 괜찮을 것이라고 생각했습니다. 그러나 나는 rounds_attributesevent_params에도 넣어야했습니다.

아래와 같이 수정하면 문제가 해결됩니다.

def event_params 
    params.fetch(:event, {}).permit(:title, :description, :start_time, :end_time, :workouts_attributes => [:id, :name, :category, :_destroy, :rounds_attributes => [:id, :weight, :set, :repetition, :_destroy]]) 
end 
+0

이 답변을 올바르게 표시해야합니다. – danilopopeye

관련 문제