在 Rails 中重命名路线(map、link_to、to_param)

发布于 2024-07-12 02:08:36 字数 1724 浏览 7 评论 0原文

我遇到了一个小问题...我设置了一个为德国网站提供服务的 Rails 应用程序。 为了利用 Rails 的内部复数功能,我将所有模型保留为英文(例如模型“JobDescription”)。 现在,如果我调用“http://mysite.com/job_descriptions/”,我会得到所有的job_descriptions....到目前为止,一切都很好。 因为我不想在我的网址中使用英文术语“job_descriptions”,所以我将以下内容放入我的routes.rb中,

map.german_term '/german_term', :controller => 'job_descriptions', :action => 'index'
map.german_term '/german_term/:id', :controller => 'job_descriptions', :action => 'show'

如果我调用“http://mysite.com/german_term/< /code>”或“http://mysite.com/german_term/283”我得到了我所有的job_descriptions,这很好。

但是,为了使 URL 更加 SEO 友好,我想将 id 替换为 URL 中更加用户友好的 slug。 因此,我将以下内容放入 job_description.rb 中:

def to_param
"#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
end

每当我在任何 link_to 方法中使用“job_description_path”时,都会呈现我的 URL类似“http://mysite/job_descriptions/13-my-job-description-标题”。

然而,这就是我陷入困境的地方,我想得到“http://mysite/german_term/13-my-job-description-title”。 我已经尝试在 link_to 代码中将“job_description_path”与“german_term_path”交换,但这只会生成“http://mysite/german_term/13< /代码>”。 显然,to_param 没有被调用。 我发现的一种解决方法是使用以下命令构建链接:

<%= link_to job_description.name, german_term_path(job_description.to_param) %>

但是更改代码中的所有 link_to 调用相当乏味。 我想要的是将 URL 中出现的“job_description”替换为“german_term”。

有什么想法吗?!?

问候,

塞巴斯蒂安

I'm having a little issue...I setup a rails application that is to serve a german website. To make use of Rails' internal pluralization features, I kept all my models in english (e.g. the model "JobDescription").
Now, if I call "http://mysite.com/job_descriptions/", I get all my job_descriptions....so far, so good. Because I didn't want the english term "job_descriptions" in my url, I put the following into my routes.rb

map.german_term '/german_term', :controller => 'job_descriptions', :action => 'index'
map.german_term '/german_term/:id', :controller => 'job_descriptions', :action => 'show'

If I call "http://mysite.com/german_term/" or "http://mysite.com/german_term/283" I get all my job_descriptions, which is fine.

However, to make the URL more SEO friendly, I'd like to exchange the id for a more userfriendly slug in the URL. Thus, I put the following in my job_description.rb:

def to_param
"#{id}-#{name.gsub(/[^a-z0-9]+/i, '-')}"
end

which, whenever I use "job_description_path" in any link_to method, renders my URLs out to something like "http://mysite/job_descriptions/13-my-job-description-title".

However, and this is where I'm stuck, I'd like to get "http://mysite/german_term/13-my-job-description-title". I already tried to exchange the "job_description_path" with "german_term_path" in the link_to code, but that only generates "http://mysite/german_term/13". Obviously, to_param isn't called.
One workaround I found is to build the link with:

<%= link_to job_description.name, german_term_path(job_description.to_param) %>

But that's rather tedious to change all the link_to calls in my code. What I want is to replace "job_description" by "german_term" whenever it occurs in a URL.

Any thoughts?!?

Regards,

Sebastian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2024-07-19 02:08:36

我认为您将需要使用宁静的路线助手来获得您想要的东西。

在这种情况下,不需要太多重构(假设您已将 JobDescriptions 映射为资源)。 保留您的 to_param 不变,并将您的 JobDescriptions 路线更改为如下所示:

map.resources :job_descriptions, :as => 'german_term'

希望这有帮助!

I think you're going to need to use the restful route helpers to get what you want.

In that case, it wouldn't take much re-factoring (assuming you've mapped JobDescriptions as a resource). Leave your to_param as is and change your JobDescriptions route to something like the following:

map.resources :job_descriptions, :as => 'german_term'

Hope this helps!

就像说晚安 2024-07-19 02:08:36

Rails 仅使用

def to_params 
end

当您使用静态路由/链接帮助器时, URL 生成器。 我知道的唯一方法就是像您那样做,除非您愿意放弃英语链接并全部用德语完成。 在这种情况下,只需删除命名路线并更改 to_params 以使用数据库中的正确名称字段即可。 此时,REST 路由应该正常运行。

Rails only utilizes the

def to_params 
end

URL builder when you are using a restful route/link helper. The only way I am aware of is to do it similar to how you did, unless you are willing to just scrap your english language links and do it all in German. In that case, just get rid of the named route lines and change the to_params to use the correct name field from the database. At that point, the REST routes should behave correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文