简体   繁体   English

为什么在运行generate / scaffold时,Rails会将名为menuitem的模型拆分为menu_item? 它破坏了我的应用程序吗?

[英]Why does rails split my model named menuitem into menu_item when i run generate/scaffold? Is it breaking my app?

When I generated a scaffold for a model class named menuitem, it generates a class file for menuitem, but in the database creates menu_item and refers to the class in the views and controller as menu_item. 当我为名为menuitem的模型类生成支架时,它为menuitem生成了一个类文件,但是在数据库中创建了menu_item,并将视图和控制器中的类称为menu_item。 This may be causing me quite a headache as im genereating a newsted show link, but its failing telling me missing method. 当生成新闻链接时,这可能使我非常头疼,但未能告诉我缺少的方法。 The rake routes tells me the show route should be menu_menu_item, but when i do: 耙路告诉我显示路线应该是menu_menu_item,但是当我这样做时:

 <td><%= link_to 'Show', menu_menu_item(@menu) %></td>

it doesnt work. 它不起作用。

Is that because of the funky two word class name? 那是因为时髦的两个单词的类名吗?

You must have generated either menu_item or menuItem or MenuItem . 您必须已生成menu_itemmenuItemMenuItem It doesn't know where one word stops and another starts unless you tell it. 除非您告诉它,否则它不知道一个词在哪里停止,另一个词在哪里开始。

Also, for your link_to s, you just need to append _path : 另外,对于您的link_to ,您只需要附加_path

<td><%= link_to 'Show', menu_menu_item_path(@menu) %></td>

Well, actually, that looks a little wrong to me. 好吧,实际上,这在我看来有点不对劲。 That looks like you're trying to go to a single item, which I think will require you to specify both the menu and the item: 看来您要转到一个项目,我认为这将需要您同时指定菜单和项目:

<td><%= link_to 'Show', menu_menu_item_path(@menu, @menu_item) %></td>

And to all the menu items in a menu: 对于菜单中的所有菜单项:

<td><%= link_to 'Show', menu_menu_items_path(@menu) %></td>

Pluralizer显示您可以根据Rails的约定来命名事物。

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

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