Added nested nutrients in ingredient form
This commit is contained in:
parent
c82f903360
commit
f039917d0e
@ -4,6 +4,7 @@ class IngredientsController < ApplicationController
|
|||||||
|
|
||||||
def index
|
def index
|
||||||
@ingredient = Ingredient.new
|
@ingredient = Ingredient.new
|
||||||
|
@nutrient = Nutrient.new
|
||||||
@ingredients = @project.ingredients
|
@ingredients = @project.ingredients
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,4 +1,10 @@
|
|||||||
module IngredientsHelper
|
module IngredientsHelper
|
||||||
|
def quantity_options
|
||||||
|
nested_set_options(@project.quantities.diet) do |q|
|
||||||
|
raw("#{' ' * q.level}#{q.name}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def unit_options
|
def unit_options
|
||||||
@project.units.map do |u|
|
@project.units.map do |u|
|
||||||
[u.shortname, u.id]
|
[u.shortname, u.id]
|
||||||
|
@ -4,6 +4,8 @@ class Ingredient < ActiveRecord::Base
|
|||||||
}
|
}
|
||||||
|
|
||||||
belongs_to :project
|
belongs_to :project
|
||||||
|
has_many :nutrients
|
||||||
|
accepts_nested_attributes_for :nutrients
|
||||||
|
|
||||||
validates :project, associated: true
|
validates :project, associated: true
|
||||||
validates :name, presence: true, uniqueness: {scope: :project_id}
|
validates :name, presence: true, uniqueness: {scope: :project_id}
|
||||||
|
@ -1,2 +1,8 @@
|
|||||||
class Nutrient < ActiveRecord::Base
|
class Nutrient < ActiveRecord::Base
|
||||||
|
belongs_to :ingredient
|
||||||
|
|
||||||
|
validates :ingredient, presence: true, associated: true
|
||||||
|
validates :quantity, presence: true, associated: true
|
||||||
|
validates :amount, numericality: {greater_than: 0}
|
||||||
|
validates :unit, presence: true, associated: true
|
||||||
end
|
end
|
||||||
|
@ -7,4 +7,11 @@
|
|||||||
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
|
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
|
||||||
</p>
|
</p>
|
||||||
<p><%= f.select :group, group_options, required: true %></p>
|
<p><%= f.select :group, group_options, required: true %></p>
|
||||||
|
<%= f.fields_for :nutrients, @nutrient do |ff| %>
|
||||||
|
<p>
|
||||||
|
<%= ff.select :quantity_id, quantity_options, {label: '', required: true} %>
|
||||||
|
<%= ff.number_field :amount, {size: 8, label: '', required: true} %>
|
||||||
|
<%= ff.select :unit_id, unit_options, {label: '', required: true} %>
|
||||||
|
</p>
|
||||||
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
@ -19,7 +19,7 @@ en:
|
|||||||
heading: 'Summary'
|
heading: 'Summary'
|
||||||
sidebar:
|
sidebar:
|
||||||
heading_body_trackers: 'Body trackers'
|
heading_body_trackers: 'Body trackers'
|
||||||
heding_diet: 'Diet'
|
heading_diet: 'Diet'
|
||||||
heading_common: 'Common'
|
heading_common: 'Common'
|
||||||
link_summary: 'Summary'
|
link_summary: 'Summary'
|
||||||
link_ingredients: 'Ingredients'
|
link_ingredients: 'Ingredients'
|
||||||
|
@ -30,8 +30,8 @@ class CreateUnits < ActiveRecord::Migration
|
|||||||
create_table :nutrients do |t|
|
create_table :nutrients do |t|
|
||||||
t.references :ingredient
|
t.references :ingredient
|
||||||
t.references :quantity
|
t.references :quantity
|
||||||
t.references :unit
|
|
||||||
t.decimal :amount
|
t.decimal :amount
|
||||||
|
t.references :unit
|
||||||
end
|
end
|
||||||
|
|
||||||
reversible do |dir|
|
reversible do |dir|
|
||||||
|
Reference in New Issue
Block a user