【问题标题】:Why am I getting NoMethod error when trying to make form with nested routing?为什么在尝试使用嵌套路由制作表单时出现 NoMethod 错误?
【发布时间】:2012-12-23 16:48:03
【问题描述】:

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

#

的未定义方法 `community_community_topics_path'

我该如何解决这个问题??

_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 %>

routes.rb

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

rake 路线 | grep community_topics

   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

【问题讨论】:

  • rake routes 告诉你什么?
  • @Ernest 我更新了我的问题。请检查

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


【解决方案1】:

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

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

这样做,你会看到rake routes 会将第一条路由从community_topic_index 更改为community_topics,让你使用community_topics_path

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

【讨论】:

  • 谢谢!如果我不想要像 example.com/shop/123/topics/ 这样的 url 商店怎么办?我应该只制作嵌套路由复数的最深层次吗?还是全部?
  • 是的,如果你想保持 shop 单数,只需更改嵌套资源即可。
  • 你的意思是嵌套资源=这里的主题?
  • 是的...保留“shop”单数,但“topics”复数。或者,您可以将主题保留为单数并使用其他答案(手动指定 community_topic_index_url 路径)
  • @Markow 非常感谢。
【解决方案2】:

您可以手动指定您的网址。只需将它传递给 url 参数,无论您希望表单从 rake 路径到达什么路径。从您的路线文件看来,community_topic_index_url 是您的发布操作。

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

【讨论】:

  • 谢谢!!我使我的路由设置单一。这应该是不正常的吗?它总是必须是复数?
  • Convention 是复数形式,因此您可能也想进行更改。
  • 谢谢。路径呢?它仍然可以是单数吗?还是 :as 和 :path 都应该是复数?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 2015-01-15
  • 2012-05-02
  • 1970-01-01
  • 2021-08-22
  • 2021-12-24
  • 2013-03-07
相关资源
最近更新 更多