Forum: Ruby on Rails help... why can't Iuse data from two tables in the same view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nick Horrigan (Guest)
on 2006-02-06 23:29
(Received via mailing list)
David

I've having exactly the same problem here and can't find a solution -
have you had any joy?

Nick Horrigan
ROL
E: removed_email_address@domain.invalid
T: 01572 725454
M: 07917 464756
W: www.rol.co.uk

This email and any files transmitted with it are confidential, may be
legally privileged and are intended solely for the use of the addressee.
If you have received this email in error you are requested to contact
the sender immediately, and not to disclose or make use of this
information. Although rol operates an active anti-virus policy, the
organisation accepts no liability for any damage caused by any virus
transmitted by this email, including any attachments. The views
contained in this email are those of the author and not necessarily
those of rol.
Craig W. (Guest)
on 2006-02-06 23:47
(Received via mailing list)
On Mon, 2006-02-06 at 21:29 +0000, Nick Horrigan wrote:
> David
>
> I've having exactly the same problem here and can't find a solution -
> have you had any joy?
>
----
I am working on this at the moment...

What has worked for me...

in myfile.rhtml

<%= this_controller.field_name %>
<%= this_controller.another_controller.field_name %>

and in

this_controller.rb

def myfile
  @this_controller This_controller.find(params[:id])
  @another_controller AnotherController.find(:all)
end

and also, you must set your has/belongs to/ etc. in the models

I hope this helps

Craig
David M. (Guest)
on 2006-02-07 00:08
(Received via mailing list)
Assume you want to render a view linking people and jobs together, for
some hypothetical application.  You've got two tables, 'people' and
'jobs', that you want to drag info from for this one form.

In your controller:

jobs_controller.rb
...
def link_people_to_jobs
  @jobs = Job.find(:all)
  @people = Person.find(:all)
  ...
end

Then, in your view:

<%
@jobs.each do
  |job|
  @people.each do
    |person|
    ...(do cool stuff here)
  end
end
%>

Note that you should *define* @jobs and @people in your controller,
not try to define them in your view.

Regards

Dave M.
This topic is locked and can not be replied to.