So I''m a newbie to Rails and attempting to add multiple models to a
form.
I found the Railscast for "Complex Forms, Part 1" at
http://railscasts.com/episodes/73-complex-forms-part-1 which is from
2007 and at the point where the moderator, Ryan, says "Let''s take
a
look at our code..." and refreshes the UI and in the Railscast we see
3 Tasks below the Project. However, when I refresh my project screen,
my code shows a "NoMethodError" in the Controller with an undefined
method for the prepsteps that were added. I''m not following the
Project/Task models, but am building a Recipe/Preparation Steps models
to learn Rails. Not sure what''s up except my controller needs a method
somewhere. I''m using Rails 2.3.4. Any sugggestions?
Thanks in advance!!
--------------------------------------
Error message:
NoMethodError in RecipesController#new
undefined method `prepsteps''
Following is my code:
recipes_controller.rb
def new
@recipe = Recipe.new
3.times { @recipe.prepsteps.build }
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @recipe }
end
end
recipes\new.html.erb
<% for prepstep in @recipe.prepsteps %>
<% fields_for "recipe[prepstep_attributes][]", prepstep do
|
prepstep_form| %>
<p>
<%= prepstep_form.text_field :step_number %>
<%= prepstep_form.text_field :step %>
</p>
<% end %>
<% end %>
recipe.rb
class Recipe < ActiveRecord::Base
validates_presence_of :name
has_many :prep_steps, :dependent => :destroy
accepts_nested_attributes_for :prep_steps
end
prep_steps.rb
class PrepSteps < ActiveRecord::Base
has_one :recipe, :dependent => :destroy
validates_presence_of :recipe
end
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
From a quick glance at your code, I see that you use prepsteps in your controller and pre_steps in your models. On Fri, Dec 11, 2009 at 5:28 AM, SousChef <sgalvarro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So I''m a newbie to Rails and attempting to add multiple models to a > form. > > I found the Railscast for "Complex Forms, Part 1" at > http://railscasts.com/episodes/73-complex-forms-part-1 which is from > 2007 and at the point where the moderator, Ryan, says "Let''s take a > look at our code..." and refreshes the UI and in the Railscast we see > 3 Tasks below the Project. However, when I refresh my project screen, > my code shows a "NoMethodError" in the Controller with an undefined > method for the prepsteps that were added. I''m not following the > Project/Task models, but am building a Recipe/Preparation Steps models > to learn Rails. Not sure what''s up except my controller needs a method > somewhere. I''m using Rails 2.3.4. Any sugggestions? > > Thanks in advance!! > -------------------------------------- > > Error message: > NoMethodError in RecipesController#new > > undefined method `prepsteps'' > > Following is my code: > recipes_controller.rb > def new > @recipe = Recipe.new > 3.times { @recipe.prepsteps.build } > > respond_to do |format| > format.html # new.html.erb > format.xml { render :xml => @recipe } > end > end > > recipes\new.html.erb > <% for prepstep in @recipe.prepsteps %> > <% fields_for "recipe[prepstep_attributes][]", prepstep do | > prepstep_form| %> > <p> > <%= prepstep_form.text_field :step_number %> > <%= prepstep_form.text_field :step %> > </p> > <% end %> > <% end %> > > recipe.rb > class Recipe < ActiveRecord::Base > validates_presence_of :name > > has_many :prep_steps, :dependent => :destroy > > accepts_nested_attributes_for :prep_steps > end > > prep_steps.rb > class PrepSteps < ActiveRecord::Base > has_one :recipe, :dependent => :destroy > > validates_presence_of :recipe > end > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- Andrei Erdoss -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Andrei,
Thanks for the quick feedback. I changed all prepsteps to prep_steps,
but now I''m getting an uninitialized constant error in my Recipe
controller for the following line:
3.times { @recipe.prep_steps.build }
BROWSER DISPLAY:
NameError in RecipesController#new
uninitialized constant Recipe::PrepStep
ERROR FROM THE CONSOLE:
NameError (uninitialized constant Recipe::PrepStep):
app/controllers/recipes_controller.rb:28:in `new''
app/controllers/recipes_controller.rb:28:in `times''
app/controllers/recipes_controller.rb:28:in `new''
-e:1:in `load''
-e:1
I took out the 3.times & curly braces & tried just the
"@recipe.prep_steps.build" and I got the same error. I changed the
prep_steps to a number of variations (prepsteps, PrepSteps, PrepStep)
and got other various errors. Any thoughts? Is the underscore the
issue?
Thanks in advance!
Seth
On Dec 11, 1:26 am, Andrei Erdoss
<erd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> From a quick glance at your code, I see that you use prepsteps in your
> controller and pre_steps in your models.
>
>
>
> On Fri, Dec 11, 2009 at 5:28 AM, SousChef
<sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > So I''m a newbie to Rails and attempting to add multiple
models to a
> > form.
>
> > I found the Railscast for "Complex Forms, Part 1" at
> >http://railscasts.com/episodes/73-complex-forms-part-1which is from
> > 2007 and at the point where the moderator, Ryan, says
"Let''s take a
> > look at our code..." and refreshes the UI and in the Railscast we
see
> > 3 Tasks below the Project. However, when I refresh my project screen,
> > my code shows a "NoMethodError" in the Controller with an
undefined
> > method for the prepsteps that were added. I''m not following
the
> > Project/Task models, but am building a Recipe/Preparation Steps models
> > to learn Rails. Not sure what''s up except my controller needs
a method
> > somewhere. I''m using Rails 2.3.4. Any sugggestions?
>
> > Thanks in advance!!
> > --------------------------------------
>
> > Error message:
> > NoMethodError in RecipesController#new
>
> > undefined method `prepsteps''
>
> > Following is my code:
> > recipes_controller.rb
> > def new
> > @recipe = Recipe.new
> > 3.times { @recipe.prepsteps.build }
>
> > respond_to do |format|
> > format.html # new.html.erb
> > format.xml { render :xml => @recipe }
> > end
> > end
>
> > recipes\new.html.erb
> > <% for prepstep in @recipe.prepsteps %>
> > <% fields_for "recipe[prepstep_attributes][]",
prepstep do |
> > prepstep_form| %>
> > <p>
> > <%= prepstep_form.text_field :step_number %>
> > <%= prepstep_form.text_field :step %>
> > </p>
> > <% end %>
> > <% end %>
>
> > recipe.rb
> > class Recipe < ActiveRecord::Base
> > validates_presence_of :name
>
> > has_many :prep_steps, :dependent => :destroy
>
> > accepts_nested_attributes_for :prep_steps
> > end
>
> > prep_steps.rb
> > class PrepSteps < ActiveRecord::Base
> > has_one :recipe, :dependent => :destroy
>
> > validates_presence_of :recipe
> > end
>
> > --
>
> > You received this message because you are subscribed to the Google
Groups
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> > To unsubscribe from this group, send email to
> >
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> Andrei Erdoss
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Or as a more general question, I reviewed the Railscast mentioned above & Ryan Bates says that the Railscast shows a way to do multiple models in one form by keeping the code tucked in the models and not the controllers. Since the Railscast was done in 2007, has a preferred method of doing multiple models in one form emerged? Is it preferable to put code in the models keeping the controllers light or is it preferred to put the code in the controllers and keep the models light? Or is this still a design preference for the developer to choose? Thanks in advance for enlightment! On Dec 12, 1:05 pm, SousChef <sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Andrei, > Thanks for the quick feedback. I changed all prepsteps to prep_steps, > but now I''m getting an uninitialized constant error in my Recipe > controller for the following line: > 3.times { @recipe.prep_steps.build } > > BROWSER DISPLAY: > NameError in RecipesController#new > uninitialized constant Recipe::PrepStep > > ERROR FROM THE CONSOLE: > NameError (uninitialized constant Recipe::PrepStep): > app/controllers/recipes_controller.rb:28:in `new'' > app/controllers/recipes_controller.rb:28:in `times'' > app/controllers/recipes_controller.rb:28:in `new'' > -e:1:in `load'' > -e:1 > > I took out the 3.times & curly braces & tried just the > "@recipe.prep_steps.build" and I got the same error. I changed the > prep_steps to a number of variations (prepsteps, PrepSteps, PrepStep) > and got other various errors. Any thoughts? Is the underscore the > issue? > Thanks in advance! > Seth > > On Dec 11, 1:26 am, Andrei Erdoss <erd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > From a quick glance at your code, I see that you use prepsteps in your > > controller and pre_steps in yourmodels. > > > On Fri, Dec 11, 2009 at 5:28 AM, SousChef <sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > So I''m a newbie to Rails and attempting to addmultiplemodelsto a > > > form. > > > > I found the Railscast for "Complex Forms, Part 1" at > > >http://railscasts.com/episodes/73-complex-forms-part-1whichis from > > > 2007 and at the point where the moderator, Ryan, says "Let''s take a > > > look at our code..." and refreshes the UI and in the Railscast we see > > > 3 Tasks below the Project. However, when I refresh my project screen, > > > my code shows a "NoMethodError" in the Controller with an undefined > > > method for the prepsteps that were added. I''m not following the > > > Project/Taskmodels, but am building a Recipe/Preparation Stepsmodels > > > to learn Rails. Not sure what''s up except my controller needs a method > > > somewhere. I''m using Rails 2.3.4. Any sugggestions? > > > > Thanks in advance!! > > > -------------------------------------- > > > > Error message: > > > NoMethodError in RecipesController#new > > > > undefined method `prepsteps'' > > > > Following is my code: > > > recipes_controller.rb > > > def new > > > @recipe = Recipe.new > > > 3.times { @recipe.prepsteps.build } > > > > respond_to do |format| > > > format.html # new.html.erb > > > format.xml { render :xml => @recipe } > > > end > > > end > > > > recipes\new.html.erb > > > <% for prepstep in @recipe.prepsteps %> > > > <% fields_for "recipe[prepstep_attributes][]", prepstep do | > > > prepstep_form| %> > > > <p> > > > <%= prepstep_form.text_field :step_number %> > > > <%= prepstep_form.text_field :step %> > > > </p> > > > <% end %> > > > <% end %> > > > > recipe.rb > > > class Recipe < ActiveRecord::Base > > > validates_presence_of :name > > > > has_many :prep_steps, :dependent => :destroy > > > > accepts_nested_attributes_for :prep_steps > > > end > > > > prep_steps.rb > > > class PrepSteps < ActiveRecord::Base > > > has_one :recipe, :dependent => :destroy > > > > validates_presence_of :recipe > > > end > > > > -- > > > > You received this message because you are subscribed to the Google Groups > > > "Ruby on Rails: Talk" group. > > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > Andrei Erdoss > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
To answer your question, I would check out the Wiki page from the Rails Best Practices gem: http://github.com/flyerhzm/rails_best_practices On Sun, Dec 13, 2009 at 12:22 AM, SousChef <sgalvarro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Or as a more general question, I reviewed the Railscast mentioned > above & Ryan Bates says that the Railscast shows a way to do multiple > models in one form by keeping the code tucked in the models and not > the controllers. Since the Railscast was done in 2007, has a > preferred method of doing multiple models in one form emerged? Is it > preferable to put code in the models keeping the controllers light or > is it preferred to put the code in the controllers and keep the models > light? Or is this still a design preference for the developer to > choose? > Thanks in advance for enlightment! > > On Dec 12, 1:05 pm, SousChef <sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi Andrei, > > Thanks for the quick feedback. I changed all prepsteps to prep_steps, > > but now I''m getting an uninitialized constant error in my Recipe > > controller for the following line: > > 3.times { @recipe.prep_steps.build } > > > > BROWSER DISPLAY: > > NameError in RecipesController#new > > uninitialized constant Recipe::PrepStep > > > > ERROR FROM THE CONSOLE: > > NameError (uninitialized constant Recipe::PrepStep): > > app/controllers/recipes_controller.rb:28:in `new'' > > app/controllers/recipes_controller.rb:28:in `times'' > > app/controllers/recipes_controller.rb:28:in `new'' > > -e:1:in `load'' > > -e:1 > > > > I took out the 3.times & curly braces & tried just the > > "@recipe.prep_steps.build" and I got the same error. I changed the > > prep_steps to a number of variations (prepsteps, PrepSteps, PrepStep) > > and got other various errors. Any thoughts? Is the underscore the > > issue? > > Thanks in advance! > > Seth > > > > On Dec 11, 1:26 am, Andrei Erdoss <erd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > From a quick glance at your code, I see that you use prepsteps in your > > > controller and pre_steps in yourmodels. > > > > > On Fri, Dec 11, 2009 at 5:28 AM, SousChef <sgalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > So I''m a newbie to Rails and attempting to addmultiplemodelsto a > > > > form. > > > > > > I found the Railscast for "Complex Forms, Part 1" at > > > >http://railscasts.com/episodes/73-complex-forms-part-1whichis from > > > > 2007 and at the point where the moderator, Ryan, says "Let''s take a > > > > look at our code..." and refreshes the UI and in the Railscast we see > > > > 3 Tasks below the Project. However, when I refresh my project > screen, > > > > my code shows a "NoMethodError" in the Controller with an undefined > > > > method for the prepsteps that were added. I''m not following the > > > > Project/Taskmodels, but am building a Recipe/Preparation Stepsmodels > > > > to learn Rails. Not sure what''s up except my controller needs a > method > > > > somewhere. I''m using Rails 2.3.4. Any sugggestions? > > > > > > Thanks in advance!! > > > > -------------------------------------- > > > > > > Error message: > > > > NoMethodError in RecipesController#new > > > > > > undefined method `prepsteps'' > > > > > > Following is my code: > > > > recipes_controller.rb > > > > def new > > > > @recipe = Recipe.new > > > > 3.times { @recipe.prepsteps.build } > > > > > > respond_to do |format| > > > > format.html # new.html.erb > > > > format.xml { render :xml => @recipe } > > > > end > > > > end > > > > > > recipes\new.html.erb > > > > <% for prepstep in @recipe.prepsteps %> > > > > <% fields_for "recipe[prepstep_attributes][]", prepstep do | > > > > prepstep_form| %> > > > > <p> > > > > <%= prepstep_form.text_field :step_number %> > > > > <%= prepstep_form.text_field :step %> > > > > </p> > > > > <% end %> > > > > <% end %> > > > > > > recipe.rb > > > > class Recipe < ActiveRecord::Base > > > > validates_presence_of :name > > > > > > has_many :prep_steps, :dependent => :destroy > > > > > > accepts_nested_attributes_for :prep_steps > > > > end > > > > > > prep_steps.rb > > > > class PrepSteps < ActiveRecord::Base > > > > has_one :recipe, :dependent => :destroy > > > > > > validates_presence_of :recipe > > > > end > > > > > > -- > > > > > > You received this message because you are subscribed to the Google > Groups > > > > "Ruby on Rails: Talk" group. > > > > To post to this group, send email to > rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > To unsubscribe from this group, send email to > > > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > <rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%252Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > > > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > Andrei Erdoss > > > > > > -- > > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > >-- Andrei Erdoss -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.