ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Continuation of issue #828 #841

Closed cjwightman closed 7 years ago

cjwightman commented 7 years ago

Plants belong to seasons. My form to create a new plant does not come from within a season so a season must be defined within the form. I currently have a drop-down menu to select the season. Oddly, regardless of what season is selected, when the new plant is created it is assigned a seemingly random season. I have a feeling the issue could involve my create method in the plant controller.

def create

@season = Season.find(params[:season_id])

@plant = Plant.new(plant_params)
@plant.season = Season.all.sample
@plant.save
redirect_to plants_path

end

I'm guessing it has something to do with the "Season.all.sample" but it breaks if I remove that and I don't know what an alternative would be.

amaseda commented 7 years ago

Would you mind pushing up your code and sending over the link?

cjwightman commented 7 years ago

https://github.com/cjwightman/project_two

amaseda commented 7 years ago

Ok, got it to work. Two things...

  1. Remove the @plant.season = Season.all.sample bit from the plant create action since you're handling season assignment in the form.

  2. Make a small tweak to the season drop down. I noticed that it was passing season: 1 and not season_id: 1 through params. We need the latter since that's what you specified in your strong params method. So the updated drop down would look like this...

<%= collection_select(:plant, :season_id, Season.all, :id, :name) %>

Let me know if that works for you!

cjwightman commented 7 years ago

Wow! So jealous of your crazy skills. Thank you so much you wizard of ruby.

amaseda commented 7 years ago

:+1: