[ACCEPTED]-has_many build method, Rails-ruby-on-rails
Accepted answer
Unit Conversion Controller for new
and create
should 2 be:
def new
@ingredient = Ingredient.find(params[:ingredient_id])
@unit_conversion = @ingredient.unit_conversions.build
end
def create
@ingredient = Ingredient.find(params[:ingredient_id])
@unit_conversion = @ingredient.unit_conversions.build(params[:unit_conversion])
if @unit_conversion.save
flash[:notice] = "Successfully created unit conversion."
redirect_to ingredient_unit_conversions_url(@ingredient)
else
render :action => 'new'
end
end
Also, this screencast is a nice resource for nested 1 resources.
has_many :unit_conversion
Should be pluralized since you're calling 6 it with
@unit_conversion = @ingredient.unit_conversions.build
your controller
def new
@ingredient = Ingredient.all
should be calling 5 #new
to setup a new Ingredient or #find
to grab an 4 existing Ingredient.
@ingredient = Ingredient.new # returns a new Ingredient
or
@ingredient = Ingredient.find(...) # returns an existing Ingredient
Which one you choose 3 is up to your requirements.
Also, this is 2 a typo, right?
belongs_to :Ingredient
You might want to lowercase 1 :ingredient
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.