I''m trying to create a cheezy "bookmarks" test app, that does nothing more than let you save bookmarks (favorites) to a database. What is idiomatic ruby for doing this? I can get the mangled, php way of doing it to work: (note the bookmark variable is an ActiveRecord object) <%= "<a href=''#{bookmark[''url'']}'' target=''#{bookmark[''target'']}''>#{bookmark[''text'']}</a>" %> Which makes my eyes cross. I''d like to do something like the following: <%= link_to bookmark[''text''], :url => bookmark[''url''], :target => bookmark[''target''] %> But of course link_to doesn''t have those options. Maybe just content_tag? <%= content_tag ''a'', bookmark[''text''], { ''href'' => bookmark[''url''], ''target'' => bookmark[''target''] } %> Keep in mind these are absolute url''s... Any help is appreciated! tia. -- Brock Weaver
On Tue, 2005-08-16 at 12:30 -0500, Brock Weaver wrote:> I''m trying to create a cheezy "bookmarks" test app, that does nothing > more than let you save bookmarks (favorites) to a database. > > What is idiomatic ruby for doing this? I can get the mangled, php way > of doing it to work: > (note the bookmark variable is an ActiveRecord object) > > <%= "<a href=''#{bookmark[''url'']}'' > target=''#{bookmark[''target'']}''>#{bookmark[''text'']}</a>" %> > > Which makes my eyes cross. I''d like to do something like the following: > > <%= link_to bookmark[''text''], :url => bookmark[''url''], :target => > bookmark[''target''] %> > > But of course link_to doesn''t have those options. Maybe just content_tag? > > <%= content_tag ''a'', bookmark[''text''], { ''href'' => bookmark[''url''], > ''target'' => bookmark[''target''] } %>Why not make a to_link method on your bookmartk object. Then in your view you could just do a <%= bookmark.to_link %> and in your bookmark model, def to_link "<a href=\"#{self[''url'']}\" target=\"#{self[''target'']}\">#{self[''text'']}</a> end Or ytou could make a helper method and pass it the bookmark you want to display. -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>
Heh, couldn''t see the forest through all the trees. I''m so used to DataRow objects in .NET, which you can''t easily extend and add methods to when doing dynamic data loads (aka not a strongly typed DataRow), I''d completely overlooked the "right" way to do it. Thank you for the icy-hot for my brain. Have a good one! On 8/16/05, Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org> wrote:> > On Tue, 2005-08-16 at 12:30 -0500, Brock Weaver wrote: > > I''m trying to create a cheezy "bookmarks" test app, that does nothing > > more than let you save bookmarks (favorites) to a database. > > > > What is idiomatic ruby for doing this? I can get the mangled, php way > > of doing it to work: > > (note the bookmark variable is an ActiveRecord object) > > > > <%= "<a href=''#{bookmark[''url'']}'' > > target=''#{bookmark[''target'']}''>#{bookmark[''text'']}</a>" %> > > > > Which makes my eyes cross. I''d like to do something like the following: > > > > <%= link_to bookmark[''text''], :url => bookmark[''url''], :target => > > bookmark[''target''] %> > > > > But of course link_to doesn''t have those options. Maybe just > content_tag? > > > > <%= content_tag ''a'', bookmark[''text''], { ''href'' => bookmark[''url''], > > ''target'' => bookmark[''target''] } %> > > Why not make a to_link method on your bookmartk object. Then in your > view you could just do a > <%= bookmark.to_link %> > > and in your bookmark model, > > def to_link > "<a href=\"#{self[''url'']}\" > target=\"#{self[''target'']}\">#{self[''text'']}</a> > end > > Or ytou could make a helper method and pass it the bookmark you want to > display. > -- > Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver http://www.circaware.com _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Steven Critchfield wrote:> Or ytou could make a helper method and pass it the bookmark you want to display.This is what helpers are there for -- placing display logic in your models is considered a sin by every major religion and most of the minor ones.
Thanks Steven, I''ll use the helper methodology. Is there a good image which depicts all the different MVC-related objects rails uses? I understand it does a lot of its "magic" based on file names and locations, but it would be nice to see it explicitly drawn out. The nice overview image on rubyonrails.com doesn''t give enough details I''m afraid. So is the following correct? 1. A controller is invoked by a request. Helpers always apply to controllers, and never models, views, or layouts. 2. The controller creates/uses model as necessary 3. The controller passes the model to the view and the view is processed. 4. If a layout is defined, the controller passes the output of the view into the layout. 5. The controller streams the result to the client. Thank you again for all your help. Also, does that "Agile" book detail this out? I''m on the verge of ordering it... On 8/16/05, Nicholas Seckar <nseckar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Steven Critchfield wrote: > > Or ytou could make a helper method and pass it the bookmark you want to display. > > This is what helpers are there for -- placing display logic in your > models is considered a sin by every major religion and most of the minor > ones. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Brock Weaver http://www.circaware.com
On Tue, 2005-08-16 at 16:23 -0400, Nicholas Seckar wrote:> Steven Critchfield wrote: > > Or ytou could make a helper method and pass it the bookmark you want to display. > > This is what helpers are there for -- placing display logic in your > models is considered a sin by every major religion and most of the minor > ones.So add that to the reasons I gave more than one way of getting the problem solved. But also understand that many objects contain a to_s function to output some form of string representation, why then couldn''t it know what it''s link would look like. Specifically if the model is HTML information, it may need to know how to format itself in certain ways. But otherwise, yes, a helper is the ideal location for otherwise nasty looking erb code. -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>
Brock Weaver wrote:> 1. A controller is invoked by a request. Helpers always apply to > controllers, and never models, views, or layouts.Helpers are used by the view. A more appropriate name might have been View Helpers. They are used to ensure that the view does not contain complex logic -- whenever you find yourself using a bunch of if''s in your templates, stop and consider using a helper. You can also use helpers for other classes. For example, I have a FunctionalTestHelper that provides a bunch of macros such as click_link, submit_form, etc. One could make an argument that these should be called mixins and not helpers...> 4. If a layout is defined, the controller passes the output of the > view into the layout.It might be easier to view the rendering process as atomic: when the controller indicates which view to render, it also specifies the layout to use, if any.> 5. The controller streams the result to the client.Well, Rails and your webserver takes care of that part for you...> Thank you again for all your help. Also, does that "Agile" book > detail this out? I''m on the verge of ordering it...If you do, you''ll get better answers than you do out of me ;-)
> Thank you again for all your help. Also, does that "Agile" book > detail this out? I''m on the verge of ordering it...If you put a value your time (and don''t prefer the bruses on your head), slap down the $30ish that the book costs and have it rushed to you. The book covers helpers in contollers, I was just working on that section and picked up a few tricks I wasn''t aware of. As everyone else says, unless you are a Rails guru -- grab the book. Ben