为什么我会收到这样的错误?
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路线显示我使用'to_param'作为社区的ID,但我还没有在我的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
routes.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
答案 0 :(得分:3)
当您使用嵌套路由时,您必须将community
传递到form_for
:
<%= form_for [@community, @community_topic], :html => { :class => 'form-horizontal' } do |f| %>
更新:或@community_topic.community
,以防您未设置@community
。
<%= form_for [@community_topic.community, @community_topic], :html => { :class => 'form-horizontal' } do |f| %>
您可以观看this episode的RailsCasts来完全了解嵌套资源。 Episode使用Rails 2作为例子,但你应该理解这个概念。