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_indexに変更されcommunity_topics、使用できるようになりますcommunity_topics_path

注: "shop" の代わりに "shops" を使用することもできます。これにより、Rails の通常の方法と一致するように URL がフォーマットされます:http://example.com/shopsなど。

于 2012-12-23T17:27:56.033 に答える
1

URL を手動で指定できます。フォームが rake ルートから行きたいルートを url パラメータに渡すだけです。ルート ファイルから、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 に答える