在脚手架中使用保留的 Rails 字

发布于 2024-08-21 06:45:29 字数 242 浏览 4 评论 0原文

我是 Rails 新手,但不是编程新手。我正在尝试创建一个案例管理应用程序,我正在运行命令

ruby script/generatescaffold Case casename:string caseid:string

这工作正常,但是因为 Case 是保留字,所以我尝试查看 localhost:3000/Cases 时出现错误

无论如何,还是我必须使用不同的名称?

提前致谢。

I'm new to rails, but not to programming. I'm trying to create a Case Management app, I'm running the command

ruby script/generate scaffold Case casename:string caseid:string

This works fine, however because Case is a reserved word I'm getting errors when trying to view localhost:3000/Cases

Is there anyway around this or do I just have to use a different name?

Thanks in advance.

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

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

发布评论

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

评论(3

甜柠檬 2024-08-28 06:45:29

有没有办法解决这个问题,或者我只需要使用不同的名称?

有些词是您无法解决的(见下文)。可能有一种方法可以解决“案例”问题,但通过更改名称会让您的生活更轻松。不要为小事担心 - 有很多真正的问题需要担心:-)

其他保留字 此处此处

祝你好运!

Is there anyway around this or do I just have to use a different name?

There are some words that you can't work around (see below). There may be a way to work around 'case' but you'll make life easier on yourself by changing the name. Don't sweat the small things - there're are plenty of real problems to worry about :-)

Other reserved words here and here

Good luck!

長街聽風 2024-08-28 06:45:29

认为你会给自己带来比其价值更多的悲伤,就我个人而言,如果可能的话,我会避免使用保留字。

这些错误特定于使用单词 case,因此如果您仍然愿意,可以将默认视图从: 更改

<% @cases.each do |case| %>
<tr>
<td><%=h case.casename %></td>
…

为:

<% @cases.each do |c| %>
<tr>
<td><%=h c.casename %></td>
…

Think you are going to cause yourself more grief than it's worth, personally I would avoid reserved words if at all possible.

The errors are specific to using the word case, so if you still want to, you can make things work if you alter the default views from:

<% @cases.each do |case| %>
<tr>
<td><%=h case.casename %></td>
…

to:

<% @cases.each do |c| %>
<tr>
<td><%=h c.casename %></td>
…
止于盛夏 2024-08-28 06:45:29

这是我看到的一个老问题,但我尝试使用这些单词的替代拼写。在此示例中,可以在案例后端使用“Kase”。另一个例子是“交易”的“Transaktion”。

至于在前端处理这个问题,内置的 i18n 支持 (http://guides.rubyonrails.org/ i18n.html)可能会让事情变得更容易。例如:

# /config/locales/en.yml
en:      
  helpers:
    label:
      kase:
        name: "Case name"

另外,我相信您可以在路由文件中使用一些技巧来修改所使用的 URL,但我之前没有测试过这些。

This is an old question I see, but I try to use alternate spellings of such words. In this example, "Kase" could be used on the backend for cases. Another example would be "Transaktion" for "transactions."

As for handling this on front end, the built-in i18n support (http://guides.rubyonrails.org/i18n.html) may make things easier. For example:

# /config/locales/en.yml
en:      
  helpers:
    label:
      kase:
        name: "Case name"

Also, I believe there are tricks you can use in the routes file to modify the URLs used, but I haven't tested any of this before.

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