简体   繁体   English

Rails引擎使用来自主应用程序的用户配置文件路径

[英]Rails engine using user profile path from main app

I have a rails engine that needs to link to a user profile. 我有一个Rails引擎,需要链接到用户个人资料。 So for example for user joe, I want to do something like link to /profile/show/joe How should the engine access the path to the user profile? 因此,例如对于用户joe,我想做类似链接到/ profile / show / joe的操作,引擎应如何访问用户配置文件的路径?

My first instinct was to add a profile_path method to the User model, but this isn't very good because the models don't usually have access to routes and it kind of breaks MVC. 我的第一个直觉是向User模型添加profile_path方法,但这不是很好,因为这些模型通常无法访问路由,并且会破坏MVC。

What is the best practice here? 最佳做法是什么?

# in your route file
map.connect 'article/:permalink', :controller => 'article', :action => 'view'
# in your views when linking
link_to "View #{article.title}", {:controller => 'designer', :action => 'view', :permalink => article.permalink}
# then in your controller you can use
@article = Article.find_by_permalink(params[:permalink])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM