命名空间 form_for 中的嵌套资源

发布于 2024-08-30 21:57:27 字数 807 浏览 5 评论 0原文

问题

form_for 帮助程序错误地确定了命名空间内嵌套资源的路径。有问题的模型分别是: Forum::ThreadForum::Reply ,分别位于我的模型目录下名为“forum”的子文件夹中。这是 Rails 3 BETA 3 中的内容。

routes.rb

  namespace :forum do
    root :to => 'threads#index'
    resources :threads do
      resources :replies
    end
  end

app/views/forum/replies/_form.html.haml

...
  - form_for [@thread, @reply] do |f|
...

app/controllers/forum/replies_controller.rb

...
  def new
    @reply = Forum::Reply.new
  end
...

错误

undefined method `forum_thread_forum_replies_path'

参考上面 _form.html.haml 中概述的行

Problem

The form_for helper incorrectly determines the path to my nested resource inside of a namespace. The models in question are: Forum::Thread and Forum::Reply respectively, located in a subfolder called "forum" under my models directory. This is in Rails 3 BETA 3.

routes.rb

  namespace :forum do
    root :to => 'threads#index'
    resources :threads do
      resources :replies
    end
  end

app/views/forum/replies/_form.html.haml

...
  - form_for [@thread, @reply] do |f|
...

app/controllers/forum/replies_controller.rb

...
  def new
    @reply = Forum::Reply.new
  end
...

Error

undefined method `forum_thread_forum_replies_path'

In reference to the line outlined above in _form.html.haml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

離人涙 2024-09-06 21:57:27

编辑解决方案以防人们不阅读反应:

<%= form_for [:admin, @person, @image] do |f| %>

旧反应:

我有一个带有管理命名空间以及人员和图像资源的项目,这就是我构建我的项目的方式Rails3 中的 form_for ,我还没有找到一种更干净的方法......

<%= form_for [@person, @image], :url => admin_person_images_path do |f| %>

Editted solution in case people don't read the reactions:

<%= form_for [:admin, @person, @image] do |f| %>

Old response:

I have a project with an admin namespace and People and Images resources, this is the way I build my form_for in rails3, I haven't found a way just yet to do it cleaner...

<%= form_for [@person, @image], :url => admin_person_images_path do |f| %>
微凉 2024-09-06 21:57:27

在 Rails 3 中,唯一对我正确有效的解决方案(对于新资源和编辑资源)是:

form_for @image, :url => url_for([:admin, @person, @image])

In Rails 3, the only solution that worked for me correctly (for both new and edit resource) was:

form_for @image, :url => url_for([:admin, @person, @image])
终陌 2024-09-06 21:57:27

@Douglas:这对我不起作用。在我看来,路线中的名称应该是复数。当我确实喜欢推荐时,错误是:

undefined method `admin_admin_person_admin_image_path' for #<#<Class:0x55976d0>:0x55a9bc8>

我的解决方案适用于新:

form_for @image, url: admin_person_images_path(@person, @image)

我的解决方案适用于编辑:

form_for @image, url: admin_person_image_path(@person, @image)

是否有任何解决方案可以将其组合为一种形式?

编辑(表单中新的嵌套命名空间路由的解决方案):
现在我在routes.rb中有以下逻辑

resources :mobile_users do
 namespace :candystore do
  resource :transactions
 end
end

new_mobile_user_candystore_transactions 的形式是

<%= form_for [@mobile_user], url: mobile_user_candystore_transactions_path(@mobile_user), method: :post do |f| %>

访问 Candystore::TransactionsController 创建方法,而不是访问 MobileUser 创建方法或 Candystore::TransactionsController 更新方法。

@Douglas: It's not working for me. In my view, the names in routes should be pluralize. When I do like recommended, the error is:

undefined method `admin_admin_person_admin_image_path' for #<#<Class:0x55976d0>:0x55a9bc8>

My solution that worked for New:

form_for @image, url: admin_person_images_path(@person, @image)

My solution that worked for Edit:

form_for @image, url: admin_person_image_path(@person, @image)

Ist there any solution to combine this in one form?

Edit (Solution for a new nested namespaced route in a form):
Now I had the following logic in the routes.rb

resources :mobile_users do
 namespace :candystore do
  resource :transactions
 end
end

The form for new_mobile_user_candystore_transactions is

<%= form_for [@mobile_user], url: mobile_user_candystore_transactions_path(@mobile_user), method: :post do |f| %>

to get to the Candystore::TransactionsController create method and not to e.g the MobileUser create method or Candystore::TransactionsController update method.

时间你老了 2024-09-06 21:57:27

上面的答案都不适合我 - 我有一个简单的类别/子类别关系,它由“媒体”名称空间包装。

所以我有命名空间的嵌套资源

  namespace :media do
    resources :categories do
      resources :subcategories
    end
  end

,也有命名空间的模型

class Media::Category < ActiveRecord::Base
end

class Media::Subcategory < ActiveRecord::Base
end

最后,我最终在子类别的 _form 中得到了这个:

<% theurl = @subcategory.new_record? ? media_category_subcategories_path(@category) : media_category_subcategory_path(@category,@subcategory)
   themethod = @subcategory.new_record? ? "POST" : "PATCH" %>
<%= form_for @subcategory, url: theurl , method: themethod do |f| %>
...
<% end %>

None of the answers above worked for me - I have a simple Category / Subcategory relationship which is wrapped by a 'Media' namespace.

So I have namespaced nested resources

  namespace :media do
    resources :categories do
      resources :subcategories
    end
  end

but also namespaced models

class Media::Category < ActiveRecord::Base
end

class Media::Subcategory < ActiveRecord::Base
end

In the end, I ended up with this in the _form for subcategories:

<% theurl = @subcategory.new_record? ? media_category_subcategories_path(@category) : media_category_subcategory_path(@category,@subcategory)
   themethod = @subcategory.new_record? ? "POST" : "PATCH" %>
<%= form_for @subcategory, url: theurl , method: themethod do |f| %>
...
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文