Skip to content

Commit 8877613

Browse files
author
Vadim Brand
committed
Include ingredients partial ajax in new recipe form
1 parent 89f3f75 commit 8877613

File tree

8 files changed

+61
-29
lines changed

8 files changed

+61
-29
lines changed

app/assets/javascripts/measures.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
// Place all the behaviors and hooks related to the matching controller here.
22
// All this logic will automatically be available in application.js.
3+
4+
$(document).on('ready', function(){
5+
$('#recipe-data form').on('submit', function(e) {
6+
e.preventDefault();
7+
var data = $(this).serialize();
8+
var url = $(this).attr("action");
9+
var $section = $(this).closest('#recipe-data');
10+
11+
$.ajax({
12+
url: url,
13+
type: 'POST',
14+
data: data
15+
})
16+
.done(function(response) {
17+
$section.append(response);
18+
});
19+
20+
});
21+
22+
});

app/controllers/measures_controller.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class MeasuresController < ApplicationController
2-
def new
3-
@recipe = Recipe.find_by(id: params[:recipe_id])
4-
@measure = Measure.new
5-
end
2+
# def new
3+
# # @recipe = Recipe.find_by(id: params[:recipe_id])
4+
# @measure = Measure.new
5+
# puts "IM IN MESURES"
6+
# end
67

78
def create
89
@recipe = Recipe.find_by(id: params[:recipe_id])
910
@measure = @recipe.measures.new(measure_deets)
1011
if @measure.save
11-
redirect_to new_recipe_measure_path(@recipe)
12+
redirect_to recipe_path(@recipe)
1213
else
1314
raise 'ur dum'
1415
end

app/controllers/ratings_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
class RatingsController < ApplicationController
22
include SessionsHelper
3+
include RatingsHelper
34

45
def create
56
get_recipe
6-
if !already_rated?
7+
if !already_rated?(@recipe)
78
@rating = @recipe.ratings.new(rating_params)
89
@rating.update_attributes(user_id: current_user.id)
910
if @rating.save

app/controllers/recipes_controller.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ def new
1111
def create
1212
@recipe = get_category.recipes.new(recipe_params)
1313
current_user.recipes << @recipe
14-
15-
if @recipe.save
16-
flash.notice = "Your recipe has been added!"
17-
18-
redirect_to "/categories/#{@category.name}", notice: "You recipe was successfully added!"
14+
if request.xhr?
15+
if @recipe.save
16+
flash.notice = "Your recipe has been added!"
17+
puts "STEP 1"
18+
render partial: 'measures/new', recipe: @recipe
19+
else
20+
render :new
21+
end
1922
else
20-
render :new
23+
if @recipe.save
24+
flash.notice = "Your recipe has been added!"
25+
26+
redirect_to "/categories/#{@category.name}", notice: "You recipe was successfully added!"
27+
else
28+
render :new
29+
end
2130
end
2231
end
2332

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module RatingHelper
1+
module RatingsHelper
22
def recipe_rating(recipe)
33
total = 0
44
recipe.ratings.each do |rating|
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</div>
1313
<div class='recipe-ingredient container'>
1414
<% if @recipe.measures %>
15-
<h4><%= @recipe.title%></h4>
15+
1616
<ul>
1717
<% @recipe.measures.each do |ingred| %>
1818
<li>
@@ -23,7 +23,7 @@
2323
<% end %>
2424
</div>
2525

26-
<%= form_for @measure, url: recipe_measures_path(@recipe) do |f|%>
26+
<%= form_for Measure.new, url: recipe_measures_path(@recipe) do |f|%>
2727
<span> <%= f.label :quantity %>
2828
<%= f.text_field :quantity %>
2929
<%= f.label :units %>

app/views/recipes/new.html.erb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@
88
<% end %>
99
<% end %>
1010

11-
<%= form_for @recipe, url: category_recipes_path do |f| %>
11+
<section id='recipe-data'>
12+
<%= form_for @recipe, url: category_recipes_path do |f| %>
1213

13-
<p><%= f.label :title %></p>
14-
<p><%= f.text_field :title %></p>
14+
<p><%= f.label :title %></p>
15+
<p><%= f.text_field :title %></p>
1516

16-
<p><%= f.label :directions %></p>
17-
<p><%= f.text_area :directions %></p>
17+
<p><%= f.label :directions %></p>
18+
<p><%= f.text_area :directions %></p>
1819

19-
<p><%= f.label :time %></p>
20-
<span><%= f.text_field :time %></span>
21-
<span><%= f.label "in minutes" %></span>
20+
<p><%= f.label :time %></p>
21+
<span><%= f.text_field :time %></span>
22+
<span><%= f.label "in minutes" %></span>
2223

23-
<p><%= f.label :difficulty %></p>
24-
<p><%= f.select :difficulty, options_for_select([['easy', 'easy'], ['medium', 'medium'], ['hard', 'hard']]) %></p><br>
24+
<p><%= f.label :difficulty %></p>
25+
<p><%= f.select :difficulty, options_for_select([['easy', 'easy'], ['medium', 'medium'], ['hard', 'hard']]) %></p><br>
26+
27+
<p><%= f.submit "Submit" %></p>
28+
<% end %>
29+
</section>
2530

26-
<%= f.submit "Add My Recipe!" %>
2731

28-
<% end %>
2932
</article>

config/routes.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
# get 'rating/edit'
66

7-
get 'measures/new'
8-
97
root 'categories#index'
108

119
get 'login', to: 'sessions#new'

0 commit comments

Comments
 (0)