Nested resources in namespace form_for

社会主义新天地 提交于 2019-11-28 05:48:23

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

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])

@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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!