show all children of object

I have an index page of 'objects A' who each have many of another 'object B'.

I want to have a link that will open a popup and list all the object B's of an object A.

I have this:

<%= link_to( "details of object B", { :controller=>"objectA", :action=>"showObjectBs", :ObjectA => ObjectA}, { :popup=>['ObjectBs', 'height=600,width=650,location=no,scrollbars=yes']} ) %>

and then in the action showObjectBs in the Object A controller:

  def showObjectBs     @ObjectBs = ObjectB.find(:all, :conditions => ['objectA_id' == @ObjectA.id])

    respond_to do |format|       format.html # show.html.erb       format.xml { render :xml => @objectB}     end   end

but I get an error:

ActiveRecord::RecordNotFound in ObjectAsController#show

Couldn't find ObjectA with ID=showObjectBs

It seems you have 3 problems with your snippets : First you need to find objectA in your database to use it Then the conditions in the find method doesn't seem correct Finally you've forgottent an 's' at the end of @objectB (And the naming convention you're using is not correct but that's not the problem)

So try with this instead :

def showObjectBs     @ObjectA = ObjectA.find(params[:id])     @ObjectBs = ObjectB.find(:all, :conditions => ['objectA_id ?', @ObjectA.id])

    respond_to do |format|       format.html # show.html.erb       format.xml { render :xml => @objectBs}     end   end

Le 06/04/2010 18:29, ES a �crit :

ES wrote:

I have an index page of 'objects A' who each have many of another 'object B'.

I want to have a link that will open a popup and list all the object B's of an object A.

class A   has_many :bs end

class B   belongs_to :a end

(AsController) def b_popup   @a = A.find(params[:id])   @bs = @a.bs end

(A index view) link_to('test',         {:controller => 'as',          :action => 'b_popup',          :id => a.id},         {:popup => [your stuff here]})

make sure your route in the link_to is available in routes.rb via map.resources for your object A

AMILIN Aurélien wrote:

So try with this instead :

def showObjectBs     @ObjectA = ObjectA.find(params[:id])     @ObjectBs = ObjectB.find(:all, :conditions => ['objectA_id ?', @ObjectA.id])

    respond_to do |format|       format.html # show.html.erb       format.xml { render :xml => @objectBs}     end   end

You should also fix your variable names. The standard ruby convention for variable names is all lower case with underscores separating words. Same goes for method names:

def show_object_bs   @object_a = ObjectA.find(params[:id])   @object_bs = ObjectB.find(:all, :conditions => ['object_a_id ?', @object_a.id]   ...   ... end

Notice also that class names begin with upper case, and use camel case to separate words.