0

Still learning rails and have been stuck on this for a while, I have a feeling it's a simple fix and I'm just not getting it.

I'm trying to use a separate model to populate a dropdown in another model's form. So a nested form. It is the type of activity done in a workout.

Here's what I have set up.

Workout Model

class Workout < ActiveRecord::Base
  belongs_to :user
  has_many :activities
  accepts_nested_attributes_for :activities
  validates :activity, presence: true
end

Workout Controller Params

def workout_params
      params.require(:workout).permit(:rating, :activity, :workout_date, :activity_id, activity_params:[:id, :title])
end

Activity Model

class Activity < ActiveRecord::Base
  belongs_to :workout
end

Activity Controller Params

def activity_params
   params.require(:activities).permit(:title, :rating, workout_params:[:id])
end

View

<%= form_for(@workout) do |f| %>
     <div class="control-group">
       <%= f. label :activity, class: 'control-label' %>
     </div>
     <div class="checkbox">
       <%= collection_select( :activity, :workout_id, Activity.all, :id, :title, {}, 
     {:multiple => false }) %>
     </div>
     <div class="actions">
       <%= f.submit %>
     </div>
<% end %>

Thanks in advance

Aaron King
  • 11
  • 2
  • Did you try plural form for activity => 'activities' in form. Check this https://stackoverflow.com/questions/22284668/rails-4-nested-attribute-in-a-form-not-saving?rq=1 – zishe Jul 04 '17 at 05:15
  • Yes. That didn't work. The options are there, but it's not saving. I updated the code to reflect that. Thanks for the answer – Aaron King Jul 04 '17 at 19:33
  • When attempting to create the new workout on submitting the form. The activity is coming through as blank. – Aaron King Jul 04 '17 at 19:57

1 Answers1

1

Fixed it. Error was in the view > collection_select. The order was wrong and i changed :activity_id to :activity

 <%= collection_select :workout, :activity, Activity.all, :id, :title, {}, {:multiple => false } %>
Aaron King
  • 11
  • 2