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

Ola Bini

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

rhtml again and add
this code after the part that displays the product type:




<%= render :partial => 'categories' %>


Notice that you??™ve added a span that is empty, and a div tag that contains the output generated
from the partial. You??™ll need these two elements later on for the Ajax-y parts of the
system. But right now, you need to alter the products_controller.rb again, to make sure
everything will be saved when you save a product. This is a little bit outside the box, because
Rails doesn??™t handle these multiple selections out of the box. In the create method, you need
to add this code directly after the call to Product.new:
if params[:product_categories]
@product.product_categories <<
ProductCategory.find(
params[:product_categories].collect(&:to_i))
end
This code adds all product_categories to the product, if there are any. You need to change
the update method a little bit more, because you now have to make sure validation takes place.
The new update method should look like this:
def update
@product = Product.find(params[:id])
intern_price
if @product.update_attributes(params[:product])
if params[:product_categories]
@product.product_categories = ProductCategory.find(
params[:product_categories].collect(&:to_i))
end
if @product.save
flash[:notice] = 'Product was successfully updated.'
redirect_to :action => 'show', :id => @product
end
end
if !@product.


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