0

我正在使用rails3。
在嵌套路由中加载新操作时,出现 NoMethod 错误。

#<#:0x0000000a067ef8> 的未定义方法“community_community_topics_path”

我怎样才能解决这个问题??

_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 %>

路线.rb

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

耙路线 | 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
4

2 回答 2

4

您应该在路由文件中使用复数形式的“主题”:

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

这样做,您会看到rake routes将第一条路线从community_topic_indexto更改为community_topics,让您使用community_topics_path

注意:您可能还想使用“shops”而不是“shop”,这样您的 URL 的格式将与 Rails 通常的方式一致:http://example.com/shops等。

于 2012-12-23T17:27:56.033 回答
1

您可以手动指定您的网址。只需将它传递给 url 参数,无论您希望表单从 rake 路由到达什么路由。从您的路线文件看来, community_topic_index_url 是您的发布操作。

<%= form_for :community_topic, url: community_topic_index_url, :html => { :class => 'form-horizontal' } do |f| %>
于 2012-12-23T17:56:22.150 回答