Forum: Ruby on Rails Strange error - bug in RoR?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0278ab41b49f02c096e09b6ed8fb0170?d=identicon&s=25 Roman Hausner (rhaus)
on 2007-06-08 22:40
I have gerated a scaffold_resource for "news" like so:

  script/generate scaffold_resource news <fields>

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 <applicationurl>/news from
most pages, but the same entry points to <applicationurl>/news/1 from
within the news/1 page and from within the edit page for news 1. Similar
it points to <applicationurl>/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?
Cb98c59a6ff22c72676e756bbd28f266?d=identicon&s=25 Jacob Atzen (Guest)
on 2007-06-08 23:00
(Received via mailing list)
Roman Hausner 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 Atzen
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-08 23:24
(Received via mailing list)
Hi --

On Fri, 8 Jun 2007, Roman Hausner 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 (http://www.manning.com/black)
    (See what readers are saying!  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)
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-08 23:32
(Received via mailing list)
Hi --

On Fri, 8 Jun 2007, Jacob Atzen 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 (http://www.manning.com/black)
    (See what readers are saying!  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)
0278ab41b49f02c096e09b6ed8fb0170?d=identicon&s=25 Roman Hausner (rhaus)
on 2007-06-09 16:58
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.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-09 19:03
(Received via mailing list)
Hi --

On Sat, 9 Jun 2007, Roman Hausner 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 (http://www.manning.com/black)
    (See what readers are saying!  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)
This topic is locked and can not be replied to.