I have built chapter 36 but am having trouble implementing the final ajax/jquery section where you add ingredients.
I have a few questions. I’m using Rails 3.2.3
Does the code on pg 129 in the code listing for the _destroy attribute use ajax?
Why do we not need any code for an ingredient controller or ingredient views? When I look at something similar in the rails 3 in action book which is attaching files for its ajax/jquery example you write controller/view code.
Why do we have to declarations of the class? What is the impact if you bundle this in the one class declaration?
class Recipe < ActiveRecord::Base
def with_blank_ingredients(n = 5)
Why do we have the method ‘with_blank_ingredients’ in the model and not use ingredients.build in the recipes_controller.rb file? This has thrown me from other examples I have seen. Is this specific to the accepts_nested_attributes_for (method?)?
Can someone kindly point me in the correct direction please. The code in the download is not complete (it doesn’t contain the <% unless ingredients_form … %> block or a completed example).
Here is my code so far.
url = "/recipes/new?number=" + $('#ingredients input').length
<%= form_for @recipe.with_blank_ingredients do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :instructions %>
<%= f.text_area :instructions %>
<%= f.fields_for(:ingredients) do |ingredients_form| %>
<%= ingredients_form.label :name %>
<%= ingredients_form.text_field :name %>
<%= ingredients_form.label :quantity %>
<%= ingredients_form.text_field :quantity %>
<%= ingredients_form.hidden_field :_destroy %>
<%= unless ingredients_form.object.new_record?
<%# end %>
<% end %>
<%= f.submit %>
<% end %>
Currently when I click the link nothing happens. I thought it was because there was no render .js in the controller statement, but I’m not so sure now. I’ve also tried to follow the old examples from Ryan Bates Railscasts but have not had luck with that either.
Any help would be much appreciated.
I tried searching on Github to see if anybody had put code up there but could not find any.
Is there a reason that the authors do not put this code up on Github? It would be easier to see the changes they have made with any errata that occur as opposed to picking up a .tar/.zip file IMHO.
As with a previous post, as a relatively new Rails programmer I would have like an explanation as to the final comment on page 130 “Before turning to a complex nested form …”, and an when its appropriate to use each of the styles (complex nested form vs. multiple steps) along with a worked example to aid my learning.
Have picked up some good tips from the book thus far in the database recipes section.