1

私には、Event,WorkoutRoundという3つのモデルがあります。ダブルネストフォームでフォームを保存するにはどうすればよいですか?

人は、運動を含むイベントを作成し、ラウンドを通してセットと体重の数を設定することができます。

現在、私はcocoon gemを使用してネストされたフォームを作成しています。フォームを使用してイベントとワークアウトを保存することはできますが、ラウンドは保存されていません。

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> 

ためのフォームを持っています。しかし、私がウェブ上でフォームを使用するとき、私はそれらを保存することはできません。


EDIT 2

これは私がevent_paramsworkout_paramsセットアップを持っているか、現在あります。

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

workout_params

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関連のために必要ではないと思った。私は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

関連する問題