---
title: "Rails不可数名词模型的问题"
author: "Dollyn"
date: "2010-04-14T22:31:05Z"
tags: []
categories: ["编程语言"]
slug: "rails"
draft: false
lastmod: "2010-04-15T23:13:25Z"
id: 644138
url: "https://dollyn.iteye.com/blog/644138"
---

参考：[http://www.iteye.com/topic/220360](http://www.iteye.com/topic/220360)

 

自动生成的fish\_path会有index和instance path的冲突。  
你可以使用fish\_index\_path和 fish\_path(@fish)来区别。

(1) 声明单复数形式，如果符合Rails已声明的规则，那么不需要再声明。  
可以在%RAILS\_APP\_ROOT%/config/routes.rb中定义，使用singular参数，也可以 在%RAILS\_APP\_ROOT%/config/initializers/inflections.rb文件中声明，后者好像是官方推崇的方式。  
默认规则文件为： 

%RUBY\_ROOT\_PATH%\\lib\\ruby\\gems\\1.8\\gems\\activesupport-2.1.0\\lib\\active\_support\\inflections.rb 

 

不过我用的是修改routes.rb的方法：

```
map.resources :news, :singular => 'news'
```

 

 

(2) 修改**modelname\_controlles.rb,  /new.html.erb,   /show.html.erb,   /edit.html.erb** 中的***modelname* \_path** 为***modelname* \_index\_path**

 

(3) 修改**new.html.erb** 中的

```
<% form_for(@modelname) do |f| %>
```

为

```
<% form_for( @modelname ,  :url  => modelname_index_path)  do  |f| %> 
```