Hello all,
I''m a RoR newbie and just wondering if anyone out there can help me. I
have a view page where I send over two instance variables like so:
Controller
def new
@category = Category.find(:all, :order => "description asc")
@product = Product.new
end
def create
@product = Product.new(params[:product])
if @product.save
flash[:notice] = ''Product was successfully created.''
redirect_to :action => ''timeframe''
else
render :action => ''new''
end
end
View page snippet:
<%= form_tag( { :action => ''create''}, :multipart =>
true ) %>
<%= error_messages_for ''product'' %>
<table cellpadding="5" cellspacing="0">
<tr>
<td>
<label for="product_category_uid">Meal
Categories</label>
</td>
<td>
<select name="product[category_uid]">
<% @category.each do |category| %>
<option value=<%= category.id %>">
<%=category.description %>
</option>
<%end%>
</select>
</td>
</tr>
Product Model Validation:
belongs_to :category, :class_name => "Category", :foreign_key
=>
"category_uid"
validates_presence_of :description, :abbreviation, :price, :staffprice
validates_numericality_of :price, :reducedprice, :staffprice
Now when I have a validation error, I get this particular error:
You have a nil object when you didn''t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each
Extracted source (around line #16):
13: </td>
14: <td>
15: <select name="product[category_uid]">
16: <% @category.each do |category| %>
17: <option value=<%= category.id %>">
18: <%=category.description %>
19: </option>
My problem is I just don''t know my way around this. I haven''t
really
found that much information on this. Could anyone please help and point
me in the right direction. Thanks for your help
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Steve Bartholomew
2007-Jan-29 23:46 UTC
Re: RoR newbie needs help with instance variables and view
The problem here is that you''re loading the categories in the
''new''
action but your form error page is shown in the ''create''
action -
notice that your ''create'' has no reference to @category in it.
Now,
you are calling ''render :action => ''new''''
which will cause the create
template to be rendered, but it will not load the categories.
You have a couple of options - you could put another ''@categories
=..''
statement in the ''create'' action, but that would mean
duplicating
code. The best options are to either use a before_filter (see http://
api.rubyonrails.org/classes/ActionController/Filters/
ClassMethods.html), or you can compress your create/new actions into
one:
def new
@categories = Category.find(:all, :order => "description asc")
if request.post?
@product = Product.new(params[:product])
if @product.save
flash[:notice] = ''Product was successfully created.''
redirect_to :action => ''timeframe''
end
else
@product = Product.new
end
end
You can then just put ''new'' as your form action.
Hope that helps,
Steve
--~--~---------~--~----~------------~-------~--~----~
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Jan 30, 10:16 am, DLo <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hello all, > > I''m a RoR newbie and just wondering if anyone out there can help me. I > have a view page where I send over two instance variables like so: > > Controller > > def new > @category = Category.find(:all, :order => "description asc") > @product = Product.new > end > def create > @product = Product.new(params[:product]) > if @product.save > flash[:notice] = ''Product was successfully created.'' > redirect_to :action => ''timeframe'' > else > render :action => ''new'' > > end > end > View page snippet: > > <%= form_tag( { :action => ''create''}, :multipart => true ) %> > > <%= error_messages_for ''product'' %> > <table cellpadding="5" cellspacing="0"> > <tr> > <td> > <label for="product_category_uid">Meal Categories</label> > </td> > <td> > <select name="product[category_uid]"> > <% @category.each do |category| %> > <option value=<%= category.id %>"> > <%=category.description %> > </option> > <%end%> > </select> > </td> > </tr> > > Product Model Validation: > > belongs_to :category, :class_name => "Category", :foreign_key => > "category_uid" > validates_presence_of :description, :abbreviation, :price, :staffprice > validates_numericality_of :price, :reducedprice, :staffprice > > Now when I have a validation error, I get this particular error: > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.each > > Extracted source (around line #16): > > 13: </td> > 14: <td> > 15: <select name="product[category_uid]"> > 16: <% @category.each do |category| %> > 17: <option value=<%= category.id %>"> > 18: <%=category.description %> > 19: </option> > > My problem is I just don''t know my way around this. I haven''t really > found that much information on this. Could anyone please help and point > me in the right direction. Thanks for your help > > -- > Posted viahttp://www.ruby-forum.com/.While you are creating the Product, you are not creating and associating the Categories, or populating the instance variable for the view. Try something like: Controller def create @product = Product.new(params[:product]) if @product.save @category = @product.category # Add this line. flash[:notice] = ''Product was successfully created.'' redirect_to :action => ''timeframe'' else render :action => ''new'' end 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks so much for the quick reply. You guys are the greateset. Appecreciate it. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---