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