Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么在尝试使用嵌套路由生成表单时会出现NoMethod错误?_Ruby On Rails_Ruby On Rails 3_Routing - Fatal编程技术网

Ruby on rails 为什么在尝试使用嵌套路由生成表单时会出现NoMethod错误?

Ruby on rails 为什么在尝试使用嵌套路由生成表单时会出现NoMethod错误?,ruby-on-rails,ruby-on-rails-3,routing,Ruby On Rails,Ruby On Rails 3,Routing,我正在使用rails3。 在嵌套路由中加载新操作时,出现NoMethod错误 未定义的方法“community\u community\u topics\u path”# 我怎样才能解决这个问题 \u form.html.erb <%= form_for ([@community, @community_topic]), :html => { :class => 'form-horizontal' } do |f| %> <div class="control

我正在使用rails3。
在嵌套路由中加载新操作时,出现NoMethod错误

未定义的方法“community\u community\u topics\u path”#

我怎样才能解决这个问题

\u form.html.erb

<%= form_for ([@community, @community_topic]), :html => { :class => 'form-horizontal' } do |f| %>

  <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_topics_path, :class => 'btn' %>
  </div>
<% end %>
resources :communities, :path => "shop", do
    resources :community_topics, :path => "topic", :as => :'topic'
end
rake路由| grep社区_主题

   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

您应该在路由文件中使用“topic”的复数形式:

resources :communities, :path => "shop", do
  resources :community_topics, :path => "topics", :as => :'topics'
end
这样做,您将看到
rake routes
将第一条路由从
community\u topic\u index
更改为
community\u topics
,允许您使用
community\u topics\u path


注意:您可能还希望使用“shop”而不是“shop”,这样您的URL的格式将与Rails通常的格式一致:
http://example.com/shops
等。

您应该在路由文件中使用“topic”的复数形式:

resources :communities, :path => "shop", do
  resources :community_topics, :path => "topics", :as => :'topics'
end
这样做,您将看到
rake routes
将第一条路由从
community\u topic\u index
更改为
community\u topics
,允许您使用
community\u topics\u path


注意:您可能还希望使用“shop”而不是“shop”,这样您的URL的格式将与Rails通常的格式一致:
http://example.com/shops
等。

您可以手动指定url。只要将它传递给url参数,无论表单从rake路由转到什么路由。从路由文件中可以看出,社区主题索引url是您的发布操作

<%= form_for :community_topic, url: community_topic_index_url, :html => { :class => 'form-horizontal' } do |f| %>
{:class=>'form horizontal'}do | f |%>

您可以手动指定url。只要将它传递给url参数,无论表单从rake路由转到什么路由。从路由文件中可以看出,社区主题索引url是您的发布操作

<%= form_for :community_topic, url: community_topic_index_url, :html => { :class => 'form-horizontal' } do |f| %>
{:class=>'form horizontal'}do | f |%>


rake routes告诉了你什么?@Ernest我更新了我的问题。请检查
rake routes
告诉你什么?@Ernest我更新了我的问题。请检查一下,谢谢!如果我不想在example.com/shop/123/topics/之类的url中使用商店,该怎么办?我应该把嵌套路由的最深层层次化吗?或者全部?是的,如果你想保持单数,只改变嵌套资源。你是说这里的嵌套资源=主题吗?是的。。。将“shop”保留为单数,但“topics”保留为复数。或者,您可以将主题保留为单数,然后使用其他答案(手动指定社区主题索引url路径)@Markow非常感谢。谢谢!如果我不想在example.com/shop/123/topics/之类的url中使用商店,该怎么办?我应该把嵌套路由的最深层层次化吗?或者全部?是的,如果你想保持单数,只改变嵌套资源。你是说这里的嵌套资源=主题吗?是的。。。将“shop”保留为单数,但“topics”保留为复数。或者,您可以将主题保留为单数,然后使用其他答案(手动指定社区主题索引url路径)@Markow非常感谢。谢谢!!我把我的路由设置设置为单数。这应该是不正常的吗?它总是必须是复数的?惯例是复数的,所以你可能也想做出改变。谢谢。路径呢?它仍然可以是单数的?或者两者:as和:path应该是复数?谢谢!!我把我的路由设置设置为单数。这应该是不正常的吗?它总是必须是复数的?惯例是复数的,所以你可能也想做出改变。谢谢。路径呢?它仍然可以是单数的?或者:as和:path都应该是复数?