Community has_many CommunityTopics
当它加载 community_topics_controller.rb # show 时,它会给出这样的错误。为什么??(例如:http ://example.com/shop/walmart/topic/12 )
它没有得到@community_topic???
路由错误没有路由匹配 {:controller=>"community_topics", :community_id=>nil}
我的代码是
community_topics_controller.rb #show
def show
@community_topic = CommunityTopic.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @community_topic }
end
end
意见/community_topics/show.html.erb
<%- model_class = @community_topic.class -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human %></h1>
</div>
<dl class="dl-horizontal">
<dt><strong><%= model_class.human_attribute_name(:community_id) %>:</strong></dt>
<dd><%= @community_topic.community_id %></dd>
<dt><strong><%= model_class.human_attribute_name(:user_id) %>:</strong></dt>
<dd><%= @community_topic.user_id %></dd>
<dt><strong><%= model_class.human_attribute_name(:title) %>:</strong></dt>
<dd><%= @community_topic.title %></dd>
<dt><strong><%= model_class.human_attribute_name(:body) %>:</strong></dt>
<dd><%= @community_topic.body %></dd>
<dt><strong><%= model_class.human_attribute_name(:community_topic_icon_file_name) %>:</strong></dt>
<dd><%= @community_topic.community_topic_icon_file_name %></dd>
<dt><strong><%= model_class.human_attribute_name(:community_topic_icon_content_type) %>:</strong></dt>
<dd><%= @community_topic.community_topic_icon_content_type %></dd>
<dt><strong><%= model_class.human_attribute_name(:community_topic_icon_file_size) %>:</strong></dt>
<dd><%= @community_topic.community_topic_icon_file_size %></dd>
<dt><strong><%= model_class.human_attribute_name(:community_topic_icon_updated_at) %>:</strong></dt>
<dd><%= @community_topic.community_topic_icon_updated_at %></dd>
<dt><strong><%= model_class.human_attribute_name(:deleted_at) %>:</strong></dt>
<dd><%= @community_topic.deleted_at %></dd>
</dl>
<div class="form-actions">
<%= link_to t('.back', :default => t("helpers.links.back")),
community_topic_index_path(@community), :class => 'btn' %>
<%= link_to t('.edit', :default => t("helpers.links.edit")),
edit_community_topic_path(@community, @community_topic), :class => 'btn' %>
<%= link_to t('.destroy', :default => t("helpers.links.destroy")),
community_topic_path(@community, @community_topic),
:method => 'delete',
:confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
:class => 'btn btn-danger' %>
</div>
路线.rb
resources :communities, :path => "shop", do
resources :community_topics, :path => "topic", :as => :'topic'
end
模型/社区.rb
def to_param
"#{community_name}"
end