jpalmieri / brewery

Make and share homebrew recipes (ones that actually make beer, irl)
http://brewery-jpalmieri.herokuapp.com
0 stars 0 forks source link

grain weights are not being saved from recipe form #2

Closed jpalmieri closed 9 years ago

jpalmieri commented 9 years ago

When I run the feature spec /spec/features/user_creates_recipes_spec.rb, I get this error:

Failure/Error: expect(current_path).to eq recipe_path(id: Recipe.first)
     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"recipes", :id=>nil} missing required keys: [:id]

And when I visit recipes/new in my browser and enter in values for the required fields [recipe name, grain names, grain weights] I get an error that the record can't be saved without grains: There was an error saving your recipe: Grains can't be blank

I assumed that this was because the grains were being saved as strings instead of numbers (which is why I added the loop at line 10 in the recipe controller), but I can't tell if this is the case. In the console, when I enter a string number as the weight and try to save it, it's saved successfully (it's somehow automatically converted to a number):

> r.grains.new(name: "grain", weight: "1")
=> #<Grain id: nil, name: "grain", weight: #<BigDecimal:7fde3290f7c8,'0.1E1',9(18)>, created_at: nil, updated_at: nil, recipe_id: nil>
> r.save
   (0.2ms)  begin transaction
  SQL (0.5ms)  INSERT INTO "recipes" ("name", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["name", "beers"], ["user_id", 1], ["created_at", "2015-01-26 23:47:54.831762"], ["updated_at", "2015-01-26 23:47:54.831762"]]
  SQL (0.1ms)  INSERT INTO "grains" ("name", "weight", "recipe_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["name", "grain"], ["weight", 1.0], ["recipe_id", 12], ["created_at", "2015-01-26 23:47:54.834927"], ["updated_at", "2015-01-26 23:47:54.834927"]]
   (0.8ms)  commit transaction
=> true

Any idea what's going on here?

jpalmieri commented 9 years ago

I have a feeling my answer is somewhere in here, but I'm having trouble understanding exactly what I should be doing: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html