为什么显示动作在嵌套时不会被正确加载

时间:2012-12-24 08:30:31

标签: ruby-on-rails ruby-on-rails-3

社区has_many CommunityTopics
当它加载community_topics_controller.rb#show时,它会给出这样的错误。为什么?? (例如:http://example.com/shop/walmart/topic/12
它没有得到 @community_topic ???

  

路由错误没有路由匹配{:controller =>“community_topics”,   :community_id =>零}

我的代码是

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>

的routes.rb

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

模型/ community.rb

def to_param
  "#{community_name}"
end

1 个答案:

答案 0 :(得分:1)

我没有看到您在@community中使用show.html.erb变量,但您的路径助手正在接收nil而不是ID。

 def show
   @community = Community.find params[:community_id]
   @community_topic = CommunityTopic.find params[:id]
   # the rest of the action
 end