has_many build method, Rails

前端 未结 2 1474
后悔当初
后悔当初 2021-02-19 09:13

Another newbie question.

The goal: each ingredient can have zero or more unit conversions tied to it. I want to put a link to creating a new unit conversion on the page

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-19 09:27

    has_many :unit_conversion
    

    Should be pluralized since you're calling it with

    @unit_conversion = @ingredient.unit_conversions.build
    

    your controller

    def new
      @ingredient = Ingredient.all
    

    should be calling #new to setup a new Ingredient or #find to grab an existing Ingredient.

    @ingredient = Ingredient.new       # returns a new Ingredient
    

    or

    @ingredient = Ingredient.find(...) # returns an existing Ingredient
    

    Which one you choose is up to your requirements.

    Also, this is a typo, right?

    belongs_to :Ingredient
    

    You might want to lowercase :ingredient

提交回复
热议问题