SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 121 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

valid?
@product_types = ProductType.find(:all)
@product_categories = ProductCategory.find(:all)
render :action => 'edit'
end
end
The big difference here is that you check if the product is valid explicitly, because it can
become invalid in two different places (in update_attributes, or save). You could do this in
other ways, but that would mean you??™d have to duplicate code in the method.
CHAPTER 4 ?–  STORE ADMINISTRATION 56
While you??™re still in the controller, you??™ll add another action that the Ajax call is supposed
to use to update the partial. The method is called categories_partial and looks like this:
def categories_partial
@product = Product.find_by_id(params[:id]) || Product.new
@product.product_type = ProductType.find(params[:tp])
@product_types = ProductType.find(:all)
@product_categories = ProductCategory.find(:all)
render :partial => 'categories'
end
You need to create a dummy product for the partial to work and set the product_type correctly.
After you??™ve done that, you fetch the needed product types and categories, and render
the partial. Nothing strange here, really. However, now you??™re slowly moving in to the part that
makes Ajax so practical. Ajax uses JavaScript to asynchronously update parts of the view.
So, what you have to do is provide a listener on the product type select box that updates the
categories partial. Once again, open up app/views/products/_form.rhtml and change the
product type parts to look like this:



<%= select 'product', 'product_type_id',
@product_types.


Pages:
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133