Preliminary MealsController#create
This commit is contained in:
parent
eb939a1be5
commit
ed6b1b9fe7
@ -19,6 +19,14 @@ class MealsController < ApplicationController
|
||||
end
|
||||
|
||||
def create
|
||||
@meal = @project.meals.new(meal_params)
|
||||
if @meal.save
|
||||
flash[:notice] = 'Created new meal'
|
||||
prepare_meals
|
||||
else
|
||||
@meal.ingredients.new if @meal.ingredients.empty?
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@ -26,6 +34,19 @@ class MealsController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def meal_params
|
||||
params.require(:meal).permit(
|
||||
:notes,
|
||||
ingredients_attributes:
|
||||
[
|
||||
:id,
|
||||
:food_id,
|
||||
:amount,
|
||||
:_destroy
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
def prepare_meals
|
||||
@meals = @project.meals.includes(:foods)
|
||||
end
|
||||
|
@ -1,8 +1,14 @@
|
||||
class Ingredient < ActiveRecord::Base
|
||||
belongs_to :composition, inverse_of: :ingredients, required: true
|
||||
belongs_to :composition, inverse_of: :ingredients, polymorphic: true, required: true
|
||||
belongs_to :food, required: true
|
||||
belongs_to :part_of, required: false
|
||||
|
||||
validates :ready_ratio, numericality: {greater_than_or_equal_to: 0.0}
|
||||
validates :amount, numericality: {greater_than_or_equal_to: 0.0}
|
||||
|
||||
after_initialize do
|
||||
if new_record?
|
||||
self.ready_ratio ||= BigDecimal.new('1.0')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,7 +1,8 @@
|
||||
class Meal < ActiveRecord::Base
|
||||
belongs_to :project, required: true
|
||||
|
||||
has_many :ingredients, as: :composition, dependent: :destroy, validate: true
|
||||
has_many :ingredients, as: :composition, inverse_of: :composition, dependent: :destroy,
|
||||
validate: true
|
||||
has_many :foods, through: :ingredients
|
||||
validates :ingredients, presence: true
|
||||
accepts_nested_attributes_for :ingredients, allow_destroy: true, reject_if: proc { |attrs|
|
||||
|
@ -10,10 +10,11 @@
|
||||
<p>
|
||||
<%= ff.hidden_field :id %>
|
||||
<%= ff.hidden_field :food_id, {class: "autocomplete-value"} %>
|
||||
<%= label_tag :food_name, index > 0 ? '' : t(:field_ingredients) %>
|
||||
<%= label_tag :food_name do %>
|
||||
<%= index > 0 ? '' : t(:field_ingredients) %><span class="required"> *</span>
|
||||
<% end %>
|
||||
<%= text_field_tag :food_name, nil, {class: "autocomplete autocomplete-label",
|
||||
style: "width: 80%;",
|
||||
required: true} %>
|
||||
style: "width: 80%;"} %>
|
||||
<%= ff.number_field :amount, {style: "width: 8%", step: :any, label: ''} %>
|
||||
<%= i.food.ref_unit.shortname if i.food %>
|
||||
<%= ff.hidden_field :_destroy %>
|
||||
@ -69,6 +70,7 @@
|
||||
new_row.find('label:first').hide();
|
||||
form.find('tr.ingredient:visible a.icon-del').show();
|
||||
autocompleteFood(new_row);
|
||||
new_row.find('input:visible:first').focus();
|
||||
}
|
||||
|
||||
function deleteIngredient() {
|
||||
|
2
app/views/meals/create.js.erb
Normal file
2
app/views/meals/create.js.erb
Normal file
@ -0,0 +1,2 @@
|
||||
$('#new-meal').empty();
|
||||
$('#meals').html('<%= j render partial: 'meals/index' %>');
|
@ -1,2 +1,2 @@
|
||||
$('#new-meal')
|
||||
.html('<%= j render partial: 'meals/new_form' %>');
|
||||
$('#new-meal').html('<%= j render partial: 'meals/new_form' %>');
|
||||
$('#food_name').focus();
|
||||
|
Reference in New Issue
Block a user