24 Apr 2012, 02:54
Generic-user-small

Mark (3 posts)

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

  1. Does this build on previous chapter around ajax? I am a little uncertain as nowhere can I see that we are using respond to blocks and then a file in the views directory. Or do all javascript files belong in assets/javascripts directory now including old style .js.erb's?

  2. Does the code on pg 129 in the code listing for the _destroy attribute use ajax?

  3. 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.

  4. model/recipe.rb 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
  has_many :ingredients
end
class Recipe
  accepts_nested_attributes_for :ingredients
  def with_blank_ingredients(n = 5)
    n.times do
      ingredients.build
    end
    self
  end
end

</code>

  1. 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?)?

  2. 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.

[app/assets/javascripts/recipes.js.coffee]

$(->
	$('a#add_ingredient_now').click(->
		url = "/recipes/new?number=" + $('#ingredients input').length
		$.get(url,
			(data)->
			  $('#ingredients').append(data)
		)
	)
)

</code>

[app/views/recipes/_form.html.erb]

<%= form_for @recipe.with_blank_ingredients do |f| %>
  <p>
  <%= f.label :name %>
  <%= f.text_field :name %>
  </p>

  <p>
  <%= f.label :instructions %>
  <%= f.text_area :instructions %>
  </p>
  <h2>Ingredients</h2>
  <p>
    <fieldset>
  <%= 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?
                 ingredients_form.check_box('_destroy') +
                   ingredients_form.label('_destroy', 'Remove')
    end %>

    <%# end %>

    <% end %>
  </fieldset>
    <%= link_to "Add Ingredient", 'javascript:', :id => "add_ingredient" %>

  </p>
  <%= f.submit %>
<% end %>

</code>

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.

Thank you.

Mark

  You must be logged in to comment