0

なぜこのようなエラーが発生するのですか?

undefined method `community_topics_path' for #<#<Class:0x00000009d79098>:0x00000009d70a38>

Extracted source (around line #1):

1: <%= form_for @community_topic, :html => { :class => 'form-horizontal' } do |f| %>
2:   <div class="control-group">
3:     <%= f.label :community_id, :class => 'control-label' %>
4:     <div class="controls">

rake routes は、コミュニティの ID に「to_param」を使用していることを示していますが、routes.rb で :community_id をまったく定義していません。rake ルートがこの :community_id を表示するのはなぜだろうか。それはおそらく、コミュニティ モデルに「to_param」を使用しているからでしょうか?? そのため、自動的にそれを検出し、:id を :community_id? に置き換えます。

new_community_topic GET    /communities/:community_id/topic/new(.:format)      community_topics#new

ルート.rb

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

ビュー/コミュニティ/_form.html.erb

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

.......

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

更新しました!!!

**レーキ ルート | grep community_topic

   community_topic_index GET    /communities/:community_id/topic(.:format)          community_topics#index
                         POST   /communities/:community_id/topic(.:format)          community_topics#create
     new_community_topic GET    /communities/:community_id/topic/new(.:format)      community_topics#new
    edit_community_topic GET    /communities/:community_id/topic/:id/edit(.:format) community_topics#edit
         community_topic GET    /communities/:community_id/topic/:id(.:format)      community_topics#show
                         PUT    /communities/:community_id/topic/:id(.:format)      community_topics#update
                         DELETE /communities/:community_id/topic/:id(.:format)      community_topics#destroy
4

1 に答える 1

3

ネストされたルーティングを使用しているので、パスしcommunityform_for:に渡す必要があります。

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

upd:または@community_topic.community設定していない場合@community

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

RailsCastsのこのエピソードを見て、ネストされたリソースを完全に理解することができます。エピソードでは例としてRails2を使用していますが、概念を理解する必要があります。

于 2012-12-23T10:56:15.260 に答える