# Strange error - bug in RoR?

**URL:** <https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357>\
**Category:** Rails\
**Created:** [June 8, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357 "2007-06-08T22:40:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rhaus](https://www.ruby-forum.com/u/rhaus)\
**Post date:** [June 8, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357/1 "2007-06-08T22:40:03Z")

</div>

I have gerated a scaffold\_resource for “news” like so:

script/generate scaffold\_resource news

I have also added a route to routes.rb

map.resources :news

I have an application-wide layout that contains a menu with the entry:

\<%= link\_to(“News”, news\_path) %\>

Now, the odd thing is that this generates /news from  
most pages, but the same entry points to /news/1 from  
within the news/1 page and from within the edit page for news 1. Similar  
it points to /2 from the show and edit pages for news 2.  
When a news\_path occurs anywhere on an news edit or show page, it does  
not link to the index action but to the show action for that specific  
record.

I have a couple of other models/views/controllers which all work fine,  
only the news part shows this odd (and definitely wrong) behavior.

I tried to add :singular =\> :news to the route definition, but that did  
not help.

Is this a bug in RoR?

---

<div class="post-metadata">

**Author:** ![rhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rhaus](https://www.ruby-forum.com/u/rhaus)\
**Post date:** [June 8, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357/2 "2007-06-08T23:00:24Z")

</div>

Roman H. wrote:

> \<%= link\_to(“News”, news\_path) %\>  
> only the news part shows this odd (and definitely wrong) behavior.
> 
> I tried to add :singular =\> :news to the route definition, but that did  
> not help.
> 
> Is this a bug in RoR?

Show us the relevant parts of your code please. When I do  
scaffold\_resource it works as expected.

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![rhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rhaus](https://www.ruby-forum.com/u/rhaus)\
**Post date:** [June 8, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357/3 "2007-06-08T23:24:26Z")

</div>

Hi –

On Fri, 8 Jun 2007, Roman H. wrote:

> I have a couple of other models/views/controllers which all work fine,  
> only the news part shows this odd (and definitely wrong) behavior.
> 
> I tried to add :singular =\> :news to the route definition, but that did  
> not help.
> 
> Is this a bug in RoR?

No. There can only be one method called news\_path, and either it is  
the singular named route or the plural one.

I would recommend:

map.resources :news, :singular =\> “news\_item”

Then you can do:

\<%= link\_to item.title, news\_item\_path(item) %\>  
\<%= link\_to “All news”, news\_path %\>

That will separate the singular from the plural, and it actually makes  
more sense linguistically too.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![rhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rhaus](https://www.ruby-forum.com/u/rhaus)\
**Post date:** [June 8, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357/4 "2007-06-08T23:32:27Z")

</div>

Hi –

On Fri, 8 Jun 2007, Jacob A. wrote:

> > I have an application-wide layout that contains a menu with the entry:
> > 
> > I have a couple of other models/views/controllers which all work fine,  
> > only the news part shows this odd (and definitely wrong) behavior.
> > 
> > I tried to add :singular =\> :news to the route definition, but that did  
> > not help.
> > 
> > Is this a bug in RoR?
> 
> Show us the relevant parts of your code please. When I do  
> scaffold\_resource it works as expected.

I agree that it does what’s expected, but that’s not what the OP wants  
🙂 If you look at views/news/show.rhtml, as produced by the  
scaffolding, you’ll see:

\<%= link\_to ‘Edit’, edit\_news\_path(@news) %\> |  
\<%= link\_to ‘Back’, news\_path %\>

That “Back” link would normally link to the index action (a plural  
GET), but in this case, news\_path has been taken up by the singular,  
and the :id field is inferred from the current environment. So it’s  
equivalent to:

news\_path(2) # for example, if params[:id] is current 2

and if you click on it, it takes you back to the show page you’re  
already on.

That’s why you need to differentiate between singular and plural news.

(As tested on Rails 1.2.3.)

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![rhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rhaus](https://www.ruby-forum.com/u/rhaus)\
**Post date:** [June 9, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357/5 "2007-06-09T16:58:36Z")

</div>

unknown wrote:

> I agree that it does what’s expected, but that’s not what the OP wants  
> 🙂 If you look at views/news/show.rhtml, as produced by the  
> scaffolding, you’ll see:
> 
> \<%= link\_to ‘Edit’, edit\_news\_path(@news) %\> |  
> \<%= link\_to ‘Back’, news\_path %\>
> 
> That “Back” link would normally link to the index action (a plural  
> GET), but in this case, news\_path has been taken up by the singular,  
> and the :id field is inferred from the current environment. So it’s  
> equivalent to:
> 
> news\_path(2) # for example, if params[:id] is current 2
> 
> and if you click on it, it takes you back to the show page you’re  
> already on.
> 
> That’s why you need to differentiate between singular and plural news.
> 
> (As tested on Rails 1.2.3.)

Thank you for the clarification – i was puzzled by the implicit passing  
of id, which changes the meaning of news\_path depending on where it  
appears. That did not seem intuitively right to me.

In any case, I have chosen to avoid the confusion and changed the naming  
alltogether.

---

<div class="post-metadata">

**Author:** ![rhaus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@rhaus](https://www.ruby-forum.com/u/rhaus)\
**Post date:** [June 9, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/strange-error-bug-in-ror/106357/6 "2007-06-09T19:03:24Z")

</div>

Hi –

On Sat, 9 Jun 2007, Roman H. wrote:

> > GET), but in this case, news\_path has been taken up by the singular,  
> > (As tested on Rails 1.2.3.)
> 
> Thank you for the clarification – i was puzzled by the implicit passing  
> of id, which changes the meaning of news\_path depending on where it  
> appears. That did not seem intuitively right to me.

It’s really the other way around: because news\_path is a singular  
resource route, and it requires an :id value, it makes use of the  
existing value. news\_path itself doesn’t change. If you try to put  
this in index.rhtml:

\<%= link\_to “Attempting link back to showing all news”, news\_path %\>

you’ll get an error, because it will be trying to execute news\_path  
but unable to find an :id value. (If my description isn’t consistent  
with what you’re getting, I’d be interested to hear about it.)

> In any case, I have chosen to avoid the confusion and changed the naming  
> alltogether.

That’s definitely the best plan.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
