1
0

Started Ingredient#create

This commit is contained in:
cryptogopher 2019-09-14 23:00:31 +02:00
parent faef248449
commit 0dcb13a065
6 changed files with 41 additions and 8 deletions

View File

@ -9,8 +9,35 @@ class IngredientsController < ApplicationController
end
def create
@ingredient = Ingredient.new(ingredient_params.update(project: @project))
if @ingredient.save
flash[:notice] = 'Created new ingredient'
redirect_to project_ingredients_url(@project)
else
@ingredients = @project.ingredients
render :index
end
end
def destroy
end
private
def ingredient_params
params.require(:ingredient).permit(
:name,
:ref_amount,
:ref_unit_id,
:group,
nutrients_attributes:
[
:id,
:quantity_id,
:amount,
:unit_id,
:_destroy
]
)
end
end

View File

@ -4,8 +4,12 @@ class Ingredient < ActiveRecord::Base
}
belongs_to :project
has_many :nutrients
belongs_to :ref_unit, class_name: 'Unit'
has_many :nutrients, inverse_of: :ingredient
accepts_nested_attributes_for :nutrients, allow_destroy: true
#reject_if: proc { |attrs|
# attrs['quantity_id'].blank? && attrs['amount'].blank?
#}
validates :project, associated: true
validates :name, presence: true, uniqueness: {scope: :project_id}

View File

@ -1,8 +1,10 @@
class Nutrient < ActiveRecord::Base
belongs_to :ingredient
belongs_to :ingredient, inverse_of: :nutrients
belongs_to :quantity
belongs_to :unit
validates :ingredient, presence: true, associated: true
validates :quantity, presence: true, associated: true
validates :quantity, presence: true, associated: true, uniqueness: {scope: :ingredient_id}
validates :amount, numericality: {greater_than: 0}
validates :unit, presence: true, associated: true
end

View File

@ -8,11 +8,11 @@
</p>
<p><%= f.select :group, group_options, required: true %></p>
<% @ingredient.nutrients.each_with_index do |n, index| %>
<%= f.fields_for :nutrient, n, index: '' do |ff| %>
<%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>
<p class="nutrient">
<%= ff.select :quantity_id, quantity_options,
label: (index > 0 ? '' : :field_nutrients) %>
<%= ff.number_field :amount, {size: 8, label: ''} %>
<%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %>
<%= ff.select :unit_id, unit_options, label: '' %>
<%= ff.check_box :_destroy, {style: "display:none", label: ''} %>
<%= link_to t(".button_delete_nutrient"), '#',

View File

@ -29,7 +29,6 @@
<tr>
<th><%= l(:field_name) %></th>
<th><%= l(:field_ref_amount) %></th>
<th><%= l(:field_ref_unit) %></th>
<th><%= l(:field_group) %></th>
<th><%= l(:field_source) %></th>
<th style="width:15%"><%= l(:field_action) %></th>
@ -39,9 +38,9 @@
<% @ingredients.each do |i| %>
<tr id="ingredient-<%= i.id %>" class="ingredient <%= 'hidden' if i.hidden %>">
<td class="ingredientname"><%= i.name %></td>
<td class="ref_amount"><%= i.ref_amount %> [<%= i.ref_unit %>]</td>
<td class="ref_amount"><%= i.ref_amount %> [<%= i.ref_unit.shortname %>]</td>
<td class="group"><%= i.group %></td>
<td class="source"><%= i.source %></td>
<td class="source"><%#= i.source %></td>
<td><%= delete_link ingredient_path(i), data: {} %></td>
</tr>
<% end %>

View File

@ -4,6 +4,7 @@ en:
field_action: 'Action'
field_ref_amount: 'Reference amount'
field_group: 'Group'
field_source: 'Source'
field_nutrients: 'Nutrients:'
field_domain: 'Domain'
field_parent_quantity: 'Parent'