Or better yet, a named route? Tried lots of searches and ugly hacks ... Thanks in advance.
On Saturday, July 29, 2006, at 11:00 PM, Brittain wrote:>Or better yet, a named route? Tried lots of searches and ugly hacks ... > >Thanks in advance. > >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsThen it''s probably time to ask... "If it''s this hard, should I be doing it this way?" In general, there is little need for models to know about things like ''url_for''. url_for is generally used when you are rendering a view, so that functionality should probably be in a controller or view. Is there a compelling reason why you need to use this function from a model? _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
Brittain wrote:> Or better yet, a named route? Tried lots of searches and ugly hacks ...If you want to do that, you''re probably trying to subvert MVC. There''s no good way to do that and be happy using Rails. Usually there is a good alternative, such as having the model construct a collection of simple data items that the view or controller can use to create the URL. I did that in my first Rails app, nothing to it. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
> If you want to do that, you''re probably trying to subvert MVC. There''s > no good way to do that and be happy using Rails. Usually there is a good > alternative, such as having the model construct a collection of simple > data items that the view or controller can use to create the URL. I did > that in my first Rails app, nothing to it.I ran into a similar dilemma a while back and I would appreciate it if you would be more specific in a situation such as this: You have a validate() method in a model and find an error. You call errors.add() and realize that it is something that the user must change on another page. Instead of writing "Ok, so if you look on the right sidebar under the section heading ''My Account'' click the blue link ''Settings'' and correct the discrepancy, then come back here and try again" you could just write "Invalid account ___ (you can fix this on your [link]User Settings[/url] page)".
Josh Susser <josh@...> writes: Thanks for the response Josh. Since you''re with Rails Core can assume there''s no way to call url_for (or something equivalent) from a model then? In the spirit of finding a more MVC way to handle this, here''s our situation. Feel free to provide a solution simpler than calling url_for: We''ve a model object, Resource, that represents a web-accessible resource in a CMS. We also have a legacy system that tracks these same resources that we''ve built some Rails glue code to synchronize. Our original design had a ResourceObserver that would update the legacy system after_save. However, the legacy system needs the fully-qualified URI. Unfortunately, neither the model, nor observer can call url_for (or our named routes) to return the URI. (Although I suppose we could expose a controller method as a webservice that would return one for a given ID, but then we''ve exposed our primary key to another system...) We considered storing the URI in the model object, but if our naming changes then we''ve persisted incorrect links and would prefer to have a method URI that would rewrite the URI whenever its needed. Particularly since that''s consistent with the semantics of our Resource model object. Looking forward to hearing your ideas.
Brittain wrote:> Thanks for the response Josh. Since you''re with Rails Core can assume > there''s no way to call url_for (or something equivalent) from a model then?Actually I''m not part of the core team. I''m just a blogger on the official Rails blog. But even so I occasionally know what I''m talking about :-)> We''ve a model object, Resource, that represents a web-accessible > resource in a CMS. We also have a legacy system that tracks these same > resources that we''ve built some Rails glue code to synchronize. > > Our original design had a ResourceObserver that would update the > legacy system after_save. However, the legacy system needs the > fully-qualified URI. Unfortunately, neither the model, nor observer > can call url_for (or our named routes) to return the URI. (Although I > suppose we could expose a controller method as a webservice that would > return one for a given ID, but then we''ve exposed our primary key to > another system...) > > We considered storing the URI in the model object, but if our > naming changes then we''ve persisted incorrect links and would > prefer to have a method URI that would rewrite the URI whenever its > needed. Particularly since that''s consistent with the semantics of our > Resource model object.I think setting up the web service is the way to go. You don''t need to expose the primary key to allow access to the Resource objects. You can have the web service controller look up resources by name, date, UUID, whatever... then generate a url for the resource in the controller. If you think about it, the url only means something to the controller that will be decoding it (with help of routes), so that controller is the only agent qualified to construct it. That''s why models don''t know about urls. That''s what''s behind the issue in your last paragraph. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/.
Brittain wrote:> Or better yet, a named route? Tried lots of searches and ugly hacks ... > > Thanks in advance.Hi! here''s one way to do it: ActionController::Integration::Session.new.url_for(:host => "", :controller => "", :action => "") cheers -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.