Bob:
Not sure that I follow what youare looking for here, but either way I
interpret it, you are looking for a shared partial.
Do this... in the app/views directory create a 'shared' directory, if
one doesn't already exist. Make it something like:
shared/location.rhtml (sorry, too old school to do the .erb thing)
In it, place a simple block:
<p><%= location.name %><br/>
<%= location.address1 %><br/>
... etc ...
</p>
Then, in a view that comes from the event controller, you can do:
<h2>Event</h2>
<%= @event.name %><br/>
<%= @event.contact_info %><br/>
<%= render :partial => "shared/location", :object => @event.location
(or :locals => {:location => @event.location}) %><br/>
... etc ...
In the location controller, you can also use it, pushing @location
into :object (or whatever).
There is more info here:
http://api.rubyonrails.org/classes/ActionView/Partials.html
and here:
http://snippets.dzone.com/posts/show/6863
If you have any questions, feel free to shoot me an email.
Thanks,
- Kevin
On Oct 1, 2009, at 6:25 PM, Robert Campbell wrote:
Hey everyone, I'm either blind to the obvious, or I'm being overly
complicated.
I created a site that tracks events. Originally (out of ease and
speed) I created a text field for the location of the event. My
users (rightly) got tired of doing the copy/paste shuffle every time
they wanted to create a new event. As there were only a handful of
locations being used I figured it would be nice to break out the
locations into their own model/controller and create a one-to-many
association (event has one location, location has many events).
This way, I can switch the location selection in the event form to a
drop-down. Easy!
The events table now has a location_id foreign key and there's a
"belongs_to :location" in the events model. The locations model has
a "has_many :events" but it doesn't seem really necessary; there's
no need or desire from the users to list the events by location.
I've added it simply because every single example of a one-to-many
association is built that way, though from what I' can tell
"has_many" does not require anything in the locations table, it only
generates a method that runs through the events table looking for a
matching id in the locations_id foreign-key column. (eg:
@location.events Follow? :) )
So to just to get things rolling, in views/events/show.html.erb I
have the following section:
<p>
<%= @event.location.name %><br />
<%= @event.location.address1 %><br />
<%= @event.location.address2 %><br />
<%= @event.location.city %>, <%= @event.location.state %><br />
<%= @event.location.zip %><br />
<%= @event.location.other %><br />
</p>
(there is also a location.label, which is a short nickname used in
the location drop-down)
Anyway, I figured it would be much more DRY to just call the "show"
view/action in the Locations controller from within the "show" view
for an event. IE, when I "show" and event, it "show"s the
location. Nice and OO right?
It's either not so easy, or I'm off in left field.
The closest I can get is <%= render 'locations/show' %> but of
course @location isn't set, so that does nothing. I tried adding
"@location = Locations.find(@event.location_id)" to the show action
in the Events controller, so that the above render would have a
@location when it got to the view, but I get the error
"uninitialized constant EventsController::Locations". (also tried
find_by_id)
Not sure what to do, but since I've been banging my head against
this for two days, I'm a bit numb. Overload a 'find' method in my
Location model (not the controller, the model)? I hate leaving it
as above, but I since the ugly works, I'll just put white-out on my
screen whenever I edit that file.
All thoughts welcome!
Bob Campbell
_______________________________________________
Bdrg-members mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/bdrg-members
--
XML is like violence, if it doesn’t solve your problem you’re not
using enough of it. —Anonymous
_______________________________________________
Bdrg-members mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/bdrg-members