Complete rails newbie essayant de commencer.Rails: méthode indéfinie `map 'pour l'ingrédient
J'ai deux classes, Ingrédient et Unité. Il y a trois unités, lb, gallons et douzaines, et chaque ingrédient a exactement une unité. Je pense avoir mis en place les associations/routes correctement. Lors de la création d'un nouvel ingrédient, je dois demander à l'utilisateur de définir les unités à partir de ces trois éléments. je une autre question pour en arriver là: Drop Down Box - Populated with data from another table in a form - Ruby on Rails
Modèle pour Ingrédients:
class Ingredient < ActiveRecord::Base
belongs_to :unit
end
Modèle pour les unités:
class Unit < ActiveRecord::Base
end
Routes:
map.resources :ingredients, :has_many => :unit_conversions
map.resources :units, :has_many => :ingredients
Ingrédients Nouveau contrôleur
def new
@ingredient = Ingredient.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @ingredient }
end
end
Ingrédients Vue:
<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 %>
L'erreur est
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
Je suis seulement trois jours au fond de RoR, donc je pense qu'il est simple!
mais mon impression de la documentation/lecture, est que si vous utilisez has_one, chaque fois que j'ajouter une nouvelle ingrédient, j'ajoute aussi une nouvelle unité. Je ne veux pas ça. Il n'y a que trois unités, j'ai juste besoin que chaque ingrédient soit associé exactement à un. – Joan
L'association 'has_one' fait cela. –