RoR newbie needs help with instance variables and view

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’ %>

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:
14:

Meal Categories <% @category.each do |category| %> "> <%=category.description %> <%end%>

15:
16: <% @category.each do |category| %>
17: <option value=<%= category.id %>">
18: <%=category.description %>
19:

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

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

On Jan 30, 10:16 am, DLo [email protected] wrote:

end
View page snippet:

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

Thanks so much for the quick reply. You guys are the greateset.
Appecreciate it.