I am implementing an admin subdomain and have googled to try and find the answer to this, however I have not found another instance.
My routes look like this for the subdomain section:
constraints :subdomain => 'admin' do
scope :module => "admin" do
resources :news, :events
match 'news', :to => 'news#index', :as => 'news'
root :to => "dashboard#index"
end
end
Events works fine, but for some reason in order for news to work I need to add a specific route to match it. It may help to show the partial where the error is generated (admin/shared/menu):
<ul>
<li><%= link_to 'Home', root_path, :class => "#{current_class?(root_path)}" %></li>
<li><%= link_to 'News', news_path, :class => "#{current_class?(news_path)}" %></li>
<li><%= link_to 'Events', events_path, :class => "#{current_class?(events_path)}" %></li>
<div class="clearboth"></div>
</ul>
And then the error if I was to remove the match route:
No route matches {:action=>"show", :controller=>"admin/news"} missing required keys: [:id]
I just don't have a clue why, any thoughts?
I found that this is an issue with rails in that the naming convention news should not be used due to plural issues, news -> new so therefore I had to rename everything to articles instead. Oversight on my part, a tad stupid.
The reason Rails gets confused is because "news" is used for both the singular and plural. news'.singularize
gives news
; and news.pluralize
also gives news
(Wikipedia has a longer description of this phenomenon; there are other words that do the same).
Rails will generate a news_path
route (plural, for the index action) and a news_path
route (singular, for the show action).
The singular route expects a News
instance; it appears that the show
action is defined later, and overwrites the index action, resulting in the strange behaviour.
The solution is simple: use the news_index_path
if you want the index action. I haven't encountered any other issues, and am still using the News
model.
ActiveAdmin fixed this issue in the same way.
If you really don't want to use News
, then you can suffix it with Item
, Entry
, Object
, or a similar word. This will leave you with a NewsItem
model with news_item_path
and news_items_path
.