2012-12-24 82 views
0

当我发布,我得到这个错误。为什么我得到未定义的方法错误?

未定义的方法`community_community_topic_url”

我的代码是

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_topics_path, :class => 'btn' %> 
    <%= link_to t('.edit', :default => t("helpers.links.edit")), 
       edit_community_topic_path(@community_topic), :class => 'btn' %> 
    <%= link_to t('.destroy', :default => t("helpers.links.destroy")), 
       community_topic_path(@community_topic), 
       :method => 'delete', 
       :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')), 
       :class => 'btn btn-danger' %> 
</div> 

community_topics_controller.rb

def create 
@community = Community.find_by_community_name(params[:community_id]) 


@community_topic = @community.community_topics.build (params[:id]) 

    respond_to do |format| 
     if @community_topic.save 
     format.html { redirect_to [@community, @community_topic], notice: 'Community topic was successfully created.' } 
     format.json { render json: [@community, @community_topic], status: :created, location: @community_topic } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @community_topic.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

的routes.rb

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

耙路线的结果

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 

UPDATE:

我得到

Routing Error 

No route matches {:action=>"show", :controller=>"community_topics"} 
错误

development.log

Processing by CommunityTopicsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"pc24BKJrqOaXxZHlRWEPQJKfvdcG9xVM98IqbJW4KgY=", "community_topic"=>{"community_id"=>"4", "user_id"=>"2", "title"=>"topic_title1", "body"=>"topic_body1"}, "commit"=>"Save Community topic", "community_id"=>"WALMART"} 
    [1m[35mCommunity Load (0.5ms)[0m SELECT `communities`.* FROM `communities` WHERE `communities`.`community_name` = 'smabrox' AND (`communities`.`deleted_at` IS NULL) LIMIT 1 
    [1m[36m (0.2ms)[0m [1mBEGIN[0m 
    [1m[35mSQL (0.8ms)[0m INSERT INTO `community_topics` (`body`, `community_id`, `community_topic_icon_content_type`, `community_topic_icon_file_name`, `community_topic_icon_file_size`, `community_topic_icon_updated_at`, `created_at`, `deleted_at`, `title`, `updated_at`, `user_id`) VALUES (NULL, 4, NULL, NULL, NULL, NULL, '2012-12-24 05:52:37', NULL, NULL, '2012-12-24 05:52:37', NULL) 
[paperclip] Saving attachments. 
    [1m[36m (4.1ms)[0m [1mCOMMIT[0m 
Completed 500 Internal Server Error in 46ms 

ActionController::RoutingError (No route matches {:action=>"show", :controller=>"community_topics"}): 
    app/controllers/community_topics_controller.rb:56:in `block (2 levels) in create' 
    app/controllers/community_topics_controller.rb:54:in `create' 

community_topics_controller.rb

def create 
    @community = Community.find_by_community_name(params[:community_id]) 
    @community_topic = @community.community_topics.build (params[:id]) 

    respond_to do |format| 
     if @community_topic.save 
     format.html { redirect_to community_topic_path[@community, @community_topic], notice: 'Community topic was successfully created.' } 
     format.json { render json: [@community, @community_topic], status: :created, location: @community_topic } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @community_topic.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

视图/ community_topics/_form.html.erb

<%= form_for :community_topic, url: community_topic_index_url, :html => { :class => 'form-horizontal' } do |f| %> 
    <div class="control-group"> 
    <%= f.label :community_id, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.number_field :community_id, :class => 'number_field' %> 
    </div> 
    </div> 
    <div class="control-group"> 
    <%= f.label :user_id, :class => 'control-label' %> 
    <div class="controls"> 
     <%= f.number_field :user_id, :class => 'number_field' %> 
    </div> 
    </div> 
    <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_topic_index_path, :class => 'btn' %> 
    </div> 
<% end %> 

community_topics_controller.rb #show

def show 
    @community = Community.find_by_community_name(params[:community_id]) 
    @community_topic = @community.community_topics 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @community_topic } 
    end 
    end 
+0

对于'@community_topic = @ community.community_topics.build(params [:id])'请使用community_topic关系显示社区模型。 –

+0

谢谢!在models/community.rb中,has_many:community_topics。在models/community_topics中。rb,belongs_to:社区 – HUSTEN

+0

甚至需要显示呈现的HTML是什么。 –

回答

1

communities_controller.rb,尝试改变

format.html { redirect_to community_topic_path[@community, @community_topic], notice: 'Community topic was successfully created.' } 

format.html { redirect_to community_topic_path(@community, @community_topic), notice: 'Community topic was successfully created.' } 

注意括号。

它工作吗?

+0

它看起来几乎工作。现在它说'针对<% - model_class = @ community_topic.class - %>的Array:Class'的未定义方法'model_name'“。为什么它没有通过@ community_topic.class获取model_class。我认为这就是为什么它不会显示刚创建的记录的问题。 – HUSTEN

3

你耙路线显示,该航线实际上是community_topic,因为你使用的:as => 'topic'

只是把它作为

community_topic_url 
+0

这看起来对我来说。 –

+0

我应该在哪里放置community_topic_url? – HUSTEN

+0

@HUSTEN使用,而不是'community_community_topic_url' –

相关问题