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 520207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:16+00:00 2026-05-13T08:07:16+00:00

Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.

  • 0

Complete rails newbie trying to get started.

I have two classes, Ingredient, and Unit. There are three units, lbs, gallons, and dozens, and each ingredient has exactly one unit. I think I got the associations/routes set up correctly.
When creating a new ingredient, I need to require the user to set the units from these three.
I used another question to get this far: Drop Down Box – Populated with data from another table in a form – Ruby on Rails

Model for Ingredients:

class Ingredient < ActiveRecord::Base
  belongs_to :unit
end

Model for Units:

class Unit < ActiveRecord::Base
end

Routes:

  map.resources :ingredients, :has_many => :unit_conversions
  map.resources :units, :has_many => :ingredients

Ingredients New controller

def new
    @ingredient = Ingredient.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @ingredient }
    end
  end

Ingredients New View:

<h1>New ingredient</h1>

<% form_for(@ingredient) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.label :needsDefrosting %><br />
    <%= f.check_box :needsDefrosting %>
  </p>
  <p>
   <%= f.label :baseName %>
   <%= f.collection_select :unit_id, @ingredient, :id, :baseName, :prompt => "Select a Base Measurement"%>
   <br />
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', ingredients_path %>

The error is

 NoMethodError in Ingredients#new

Showing app/views/ingredients/new.html.erb where line #16 raised:

undefined method `map' for #<Ingredient:0x3dae1c0>

Extracted source (around line #16):

13:   </p>
14:   <p>
15:    <%= f.label :baseName %>
16:    <%= f.collection_select :unit_id, @ingredient, :id, :baseName, :prompt => "Select a Base Measurement"%>
17:    <br />
18:   </p>
19:   <p>

RAILS_ROOT: C:/Users/joan/dh

I’m only about three days deep in RoR, so I suspect it’s simple!

  • 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-13T08:07:16+00:00Added an answer on May 13, 2026 at 8:07 am

    collection_select needs an array of options, you are passing an ingredient. Changing @ingredient to Unit.all should fix it.

    %= f.collection_select :unit_id, Unit.all, :id, :baseName, :prompt => "Select a Base Measurement"%>
    

    As a side note if you are only ever going to have 3 types of units it might make more sense to create a constant instead of having a table for units. This would reduce the number of joins and make the overall model a little simpler.

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

Sidebar

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.