Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6568101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:24:56+00:00 2026-05-25T14:24:56+00:00

I have a Recipe model, which has Ingredients embedded in it, using Mongoid. class

  • 0

I have a Recipe model, which has Ingredients embedded in it, using Mongoid.

class Recipe
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title, :type => String
  embeds_many :ingredients

  accepts_nested_attributes_for :ingredients, :reject_if => lambda { |a| a[:title].blank? }, :allow_destroy => true

  validates :title, :presence => true
end

class Ingredient
  include Mongoid::Document
  field :name, :type => String
  field :quantity, :type => String

  embedded_in :recipe, :inverse_of => :ingredients
end

I want to be able to create a new recipe, and the associated ingredients for that recipe, at the same time, but I’m struggling to understand how I’d go about doing this. This is what I have thus far:

_form.html.erb – Used in Recipe views

<%= form_for @recipe do |f| %>  
...
  <li>Title: <%= f.text_field :title %></li>

  <% f.fields_for :ingredients do |builder| %>
    <%= render "ingredient_fields", :f => builder %>
  <% end %>
...
<%= f.submit %>

_ingredient_fields.html.erb

<%= f.text_field :name %>

Recipe Controller

def new
  @recipe = Recipe.new
  @ingredient = @recipe.ingredients.build
end

def create
  @recipe = Recipe.new(params[:recipe])


  if @recipe.save
    redirect_to @recipe, notice: 'Recipe was successfully created.'
  else
    render action: "new"
  end
end

Ingredients Controller

def new
  @recipe = Recipe.find(params[:recipe_id])
  @ingredient = @recipe.ingredients.build
end

def create
  @recipe = Recipe.find(params[:recipe_id]) 
  @ingredient = @recipe.ingredients.build(params[:ingredient]) 
  # if @recipe.save 
end

This renders the new ingredients form, but there are no fields for the ingredients. Can anyone give me any pointers as to what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:24:57+00:00Added an answer on May 25, 2026 at 2:24 pm

    When you show the nested form, try using (notice the equals):

    <%= f.fields_for
    

    Instead of just

    <% f.fields_for
    

    See this similar question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30,
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a base recipe class and I am using a datacontext. I overrode
I have some old rrdtool databases, for which the exact creation recipe has long
sorry for my english, I have a model named Recipe and Recipe has an
So I have an observer which looks like this: class RecipeObserver < Mongoid::Observer def
I have created a model POCO class called Recipe ; a corresponding RecipeRepository persists
I have a model with a custom property class RecipeIngredient(models.Model): recipe = models.ForeignKey(Recipe) ingredient
I have a capistrano deployment recipe I've been using for some time to deploy
Each Lucene doc is a recipe, and each of these recipes have ingredients. Im

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.