te pongo el código, me sigue dando error, a ver si me puedes ayudar que me
estoy volviendo loco, gracias
el controlador
class ProductsController < ApplicationController
uses_tiny_mce
def index
@products = Product.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
end
# GET /products/1
# GET /products/1.xml
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @product }
end
end
# GET /products/new
# GET /products/new.xml
def new
@product = Product.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @product }
end
end
# GET /products/1/edit
def edit
@product = Product.find(params[:id])
@categories = Category.all
@productsimages = Productsimage.where(:id_product =>@product.id)
end
# POST /products
# POST /products.xml
def create
@product = Product.new(params[:product])
respond_to do |format|
if @product.save
format.html { redirect_to(@product, :notice => 'Product was
successfully created.') }
format.xml { render :xml => @product, :status => :created, :location
=> @product }
else
format.html { render :action => "new" }
format.xml { render :xml => @product.errors, :status =>
:unprocessable_entity }
end
end
end
# PUT /products/1
# PUT /products/1.xml
def update
@product = Product.find(params[:id])
respond_to do |format|
if @product.update_attributes(params[:product])
format.html { redirect_to(@product, :notice => 'Product was
successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @product.errors, :status =>
:unprocessable_entity }
end
end
end
# DELETE /products/1
# DELETE /products/1.xml
def destroy
@product = Product.find(params[:id])
@product.destroy
respond_to do |format|
format.html { redirect_to(products_url) }
format.xml { head :ok }
end
end
end
la vista
<h1>Listing products</h1>
<table>
<tr>
<th>Name</th>
<th>Category</th>
<th>Category</th>
<th>Ingredients</th>
<th>Expiration</th>
<th>Conservation</th>
<th>Format</th>
<th>Specialformat</th>
<th>Preparation</th>
<th>Moreinfo</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @products.each do |product| %>
<tr>
<td><%= product.name %>
</td>
<td><%= product.category_id %></td>
<td><%= product.category %></td>
<% product.productsimages.each do |productsimage| %>
<td><%= image_tag productsimage.url %>
<% end %>
<td><%=raw product.ingredients %></td>
<td><%= product.expiration %></td>
<td><%= product.conservation %></td>
<td><%= product.format %></td>
<td><%= product.specialformat %></td>
<td><%= product.preparation %></td>
<td><%= product.moreinfo %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
<td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method =>
:delete %></td>
</tr>
<% end %>
<tr>
<td>
</td>
</tr>
</table>
<br />
<% # will_paginate @products %>
<%= link_to 'New Product', new_product_path %>
los modelos
category
class Category < ActiveRecord::Base
has_many :products
def to_s
name
end
end
product
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :productsimages
has_attached_file :image, :styles => { :medium => "300x300>", :small =>
"48x48>", :thumb => "100x100>" }
# def self.search(search, page)
# paginate :per_page => 5, :page => page,
# :order => 'name'
# paginate :per_page => 5, :page => page,
# :conditions => ['name like ?', "%#{search}%"],
# :order => 'name'
#end
end
productsimages
class Productsimage < ActiveRecord::Base
has_many :product
has_attached_file :image, :styles => { :medium => "300x300>", :small =>
"48x48>", :thumb => "100x100>" }
end
base de datos
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :categories
end
end
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :name
t.integer :category_id
t.string :ingredients
t.string :expiration
t.string :conservation
t.string :format
t.string :specialformat
t.string :preparation
t.string :moreinfo
t.timestamps
end
end
def self.down
drop_table :products
end
end
class AddImageToProducts < ActiveRecord::Migration
def self.up
add_column :products, :image_file_name, :string
add_column :products, :image_content_type, :string
add_column :products, :image_file_size, :integer
add_column :products, :image_updated_at, :datetime
end
def self.down
remove_column :products, :image_file_name
remove_column :products, :image_content_type
remove_column :products, :image_file_size
remove_column :products, :image_updated_at
end
end
class CreateProductsimages < ActiveRecord::Migration
def self.up
create_table :productsimages do |t|
t.string :image_file_name
t.string :image_content_type
t.integer :image_file_size
t.datetime :image_updated_at
t.timestamps
end
end
def self.down
drop_table :productsimages
end
end
class AddIdprodToProductsimages < ActiveRecord::Migration
def self.up
add_column :productsimages, :id_product, :integer
end
def self.down
remove_column :productsimages, :id_product
end
end
El 28/07/2011, a las 14:47, Tute escribió:
> Te reitero Simula, NO tenes que hacer los JOINS vos. Eso lo hace solo
> ActiveRecord. El join se usa para casos determinados, no para este tan simple.
>
> Pasa también la linea donde da el error de nil.
>
>
>
> On 07/28/2011 09:46 AM, simula wrote:
>>
>> gracias por tu ayuda.
>> He intentando hacer esto en el controlador
>> @products = Product.paginate :per_page => 5, :page => params[:page], :joins
>> => " inner join productsimages "
>>
>> hasta aqui bien
>>
>>
>> pero al ir a pintar los datos me dice
>>
>> You have a nil object when you didn't expect it!
>> You might have expected an instance of Array.
>> The error occurred while evaluating nil.each
>>
>>
>>
>> <% @products.each do |product| %>
>>
>>
>> <tr>
>> <td><%= product.name %>
>>
>> </td>
>> <td><%= product.category_id %></td>
>> <td><%= product.category %></td>
>> <% product.productsimages.each do |productsimage| %>
>> <td><%= image_tag productsimage.url %>
>> <% end %>
>>
>> <td><%=raw product.ingredients %></td>
>> <td><%= product.expiration %></td>
>> <td><%= product.conservation %></td>
>> <td><%= product.format %></td>
>> <td><%= product.specialformat %></td>
>> <td><%= product.preparation %></td>
>> <td><%= product.moreinfo %></td>
>> <td><%= link_to 'Show', product %></td>
>> <td><%= link_to 'Edit', edit_product_path(product) %></td>
>> <td><%= link_to 'Destroy', product, :confirm => 'Are you sure?', :method
>> => :delete %></td>
>>
>> </tr>
>> <% end %>
>>
>>
>>
>> sabes que puedo estar haciendo mal?
>>
>>
>> muchas gracias
>>
>>
>>
>> El 28/07/2011, a las 13:45, Tute escribió:
>>
>>> Simula,
>>>
>>> Cuando haces un Product.all, también trae las imagenes en la medida que
>>> tengas:
>>>
>>> class Product < ActiveRecord::Base
>>>
>>> has_many :images
>>>
>>> end
>>>
>>> class Image < ActiveRecord::Base
>>> belongs_to :product
>>> end
>>>
>>> Entonces en el controlador te quedaria
>>>
>>>
>>> def [como se llame tu método]
>>> @products = Products.all, :include => [:images] #el include no es
>>> necesario, solo hace que las imagenes se carguen ahora mismo. Esto hace el
>>> inner join por vos.
>>>
>>> En la vista, vas a tener que hacer algo así como:
>>>
>>> <table>
>>> <thead>ble ble</thead>
>>> <tbody>
>>> <% @products.each do |product| %>
>>> <tr>
>>> <td><%= product.name%>
>>> <% product.images.each do |image| %>
>>> <td><%= image_tag image.url %>
>>> <% end %>
>>> </tr>
>>> <% end %>
>>> </tbody>
>>> </table>
>>>
>>> On 07/28/2011 02:33 AM, simula wrote:
>>>>
>>>> Hola Juan Pablo gracias, ya me he suscrito rubysur.
>>>>
>>>> Lo que me comentas de la vista, el problema es que no se como obtener los
>>>> datos ya que tengo una tabla con productos y otra con imágenes.
>>>>
>>>> Un producto puede tener mas de una imagen y en el controlador con
>>>> productos.all obtengo productos y no se como hacer una unión entre un
>>>> producto y sus imágenes
>>>>
>>>>
>>>> muchas gracias por la ayuda
>>>>
>>>>
>>>> El 28/07/2011, a las 00:20, Juan Pablo Taulamet escribió:
>>>>
>>>>> Hola Simula!
>>>>>
>>>>> Te cuento que muchos estamos en la lista de RubySur y ya no usamos mucho
>>>>> que digamos esta lista.
>>>>>
>>>>> Creo que para lo que estás queriendo hacer, lo más indicado sería no
>>>>> incluir las imágenes y simplemente llamarlas en la vista:
>>>>>
>>>>> Por ejemplo:
>>>>>
>>>>> Controller:
>>>>> @productos = Producto.all
>>>>>
>>>>> Vista:
>>>>> @productos.each do | producto|
>>>>> <%= producto.nombre %>
>>>>> <%= image_tag(producto.image) unless producto.image.nil? %>
>>>>>
>>>>> Quizá si mandás un correo a la lista de RubySur obtenés más y mejores
>>>>> respuestas :)
>>>>>
>>>>> :nueva_lista => [email protected]
>>>>>
>>>>> Un abrazo!
>>>>>
>>>>> --
>>>>> Saludos Cordiales,
>>>>> Juan Pablo
>>>>>
>>>>>
>>>>> 2011/7/27 simula <[email protected]>
>>>>> Hola, estoy empezando con ruby on rails y tengo una tabla de productos y
>>>>> otra de imagenes de productos
>>>>>
>>>>> a la hora de mostrar productos en el controlador hago esto
>>>>>
>>>>> @products = Product.paginate :per_page => 5, :page => params[:page]
>>>>>
>>>>> me muestra los productos paginados
>>>>>
>>>>>
>>>>> y quiero mostrar las imágenes de cada producto
>>>>>
>>>>> he intentado esto
>>>>>
>>>>> #@products = Product.paginate :per_page => 5, :page => params[:page],
>>>>> :include => " productsimages ", :conditions =>
>>>>> 'products.id=productsimage.product_id'
>>>>>
>>>>>
>>>>> y claro así me muestra sólo los productos que tienen imagen.
>>>>>
>>>>> Alguien me puede ayudar que tengo que hacer?
>>>>>
>>>>> Llevo varios días y no se como seguir
>>>>>
>>>>> gracias
>>>>> _______________________________________________
>>>>> Ruby mailing list
>>>>> [email protected]
>>>>> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>>>>>
>>>>> _______________________________________________
>>>>> Ruby mailing list
>>>>> [email protected]
>>>>> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>>>>
>>>>
>>>> _______________________________________________
>>>> Ruby mailing list
>>>> [email protected]
>>>> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>>>
>>> _______________________________________________
>>> Ruby mailing list
>>> [email protected]
>>> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>>
>>
>> _______________________________________________
>> Ruby mailing list
>> [email protected]
>> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
>
> _______________________________________________
> Ruby mailing list
> [email protected]
> http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar
_______________________________________________
Ruby mailing list
[email protected]
http://lista.rubyargentina.com.ar/listinfo.cgi/ruby-rubyargentina.com.ar