开发者

use reserved rails word in scaffolding

开发者 https://www.devze.com 2022-12-19 17:35 出处:网络
I\'m new to rails, but not to programming. I\'m trying to create a Case Management app, I\'m running the command

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 a开发者_如何学编程round this or do I just have to use a different name?

Thanks in advance.


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!


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>
…


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.

0

精彩评论

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