當我發佈,我得到這個錯誤。爲什麼我得到未定義的方法錯誤?
未定義的方法`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
對於'@community_topic = @ community.community_topics.build(params [:id])'請使用community_topic關係顯示社區模型。 –
謝謝!在models/community.rb中,has_many:community_topics。在models/community_topics中。rb,belongs_to:社區 – HUSTEN
甚至需要顯示呈現的HTML是什麼。 –