开发者

Better solution than accessing routes in the Model?

开发者 https://www.devze.com 2023-01-05 23:20 出处:网络
So I know that you shouldn\'t use UrlWriter methods in a model, but I wasn\'t sure what a better way to handle my current situation is.

So I know that you shouldn't use UrlWriter methods in a model, but I wasn't sure what a better way to handle my current situation is.

Basically, I want开发者_运维百科 to allow a User to post something to his facebook feed, and want to write a post_to_fb_feed(object) method in class User. Now, the URL of what is actually posted depends on the object, so I also have a to_fb_feed_item method on the object classes that a user can post. The to_fb_feed_item method just returns a hash that the Facebook API expects, including the url the post should link to.

I've gotten this to work currently, by including ActionController:UrlWriter in my models, but I was wondering if anybody had a better suggestion for how to handle this.

Thanks! Eric


including ActionController:UrlWriter is the best way to handle it. I don't know why it's not easier to generate urls from arbitrary places in Rails -- sure it might be more common for the appropriate place to be in controllers and views, but the fact of the matter is many models validly deal with urls as data, and need to generate them.

0

精彩评论

暂无评论...
验证码 换一张
取 消