0

I'm using rails3.
When loading new action in nested routing, I get NoMethod Error.

undefined method `community_community_topics_path' for #<#:0x0000000a067ef8>

How can I fix this??

_form.html.erb

<%= form_for ([@community, @community_topic]), :html => { :class => 'form-horizontal' } do |f| %>

  <div class="control-group">
    <%= f.label :title, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :title, :class => 'text_field' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :body, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_area :body, :class => 'text_area' %>
    </div>
  </div>

  <div class="form-actions">
    <%= f.submit nil, :class => 'btn btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                community_topics_path, :class => 'btn' %>
  </div>
<% end %>

routes.rb

resources :communities, :path => "shop", do
    resources :community_topics, :path => "topic", :as => :'topic'
end

rake routes | grep community_topics

   community_topic_index GET    /shop/:community_id/topic(.:format)          community_topics#index
                         POST   /shop/:community_id/topic(.:format)          community_topics#create
     new_community_topic GET    /shop/:community_id/topic/new(.:format)      community_topics#new
    edit_community_topic GET    /shop/:community_id/topic/:id/edit(.:format) community_topics#edit
         community_topic GET    /shop/:community_id/topic/:id(.:format)      community_topics#show
                         PUT    /shop/:community_id/topic/:id(.:format)      community_topics#update
                         DELETE /shop/:community_id/topic/:id(.:format)      community_topics#destroy
2
  • What rake routes tells you? Commented Dec 23, 2012 at 16:56
  • @Ernest I updated my Question. Please check Commented Dec 23, 2012 at 17:14

2 Answers 2

4

You should be using the plural form of "topic" in your routes file:

resources :communities, :path => "shop", do
  resources :community_topics, :path => "topics", :as => :'topics'
end

Do that, and you'll see rake routes will change the first route from community_topic_index to community_topics, letting you use community_topics_path

Note: you may also want to use "shops" instead of "shop", that way your URLs will be formatted consistent to how Rails usually does it: http://example.com/shops, etc.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! What if I dont want shops for url like example.com/shop/123/topics/? should I just make the deepest hierarhy of nested routing plural? or all of them?
Yeah, if you want to keep shop singular, only change the nested resource.
do you mean that the nested resource = topics here?
Yes... leave "shop" singular, but "topics" plural. Or, you can leave topics singular and use the other answer (manually specifying the community_topic_index_url path)
1

You can specify your url manually. Just pass it to the url param whatever route you want for the form to go to from rake routes. It looks like from your routes file that community_topic_index_url is your post action.

<%= form_for :community_topic, url: community_topic_index_url, :html => { :class => 'form-horizontal' } do |f| %>

3 Comments

Thanks!! I made my routing setup singular. Is this supposed to be abnormal? It always has to be plural?
Convention is to be plural so you might want to make that change as well.
Thanks. what about path? it still can be singular? or both :as and :path should be plural?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.