I am using Rails 4.1 and Mongoid 4.0 and I'm pretty new to both.
I am trying to do a simple N-N referenced relationship between an Ingredients class and a Recipe class.
I have this:
class Recipe
include Mongoid::Document
field :name, type: String
has_and_belongs_to_many :ingredients
end
class Ingredient
include Mongoid::Document
field :name, type: String
field :price, type: BigDecimal
has_and_belongs_to_many :recipes
end
def create
@recipe = Recipe.new(recipe_params)
respond_to do |format|
if @recipe.save
format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
format.json { render :show, status: :created, location: @recipe }
else
format.html { render :new }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end
def recipe_params
params.require(:recipe).permit(:name, :ingredient_ids)
end
Recipes Form:
<div class="field">
<%= f.label :ingredients %><br />
<%= f.collection_select :ingredient_ids, Ingredient.all, :id, :name %>
</div>
I believe that the relationship is set up correctly but I don't understand the error.
p paramsand and paste that output here, alsop recipe_params