Nested resources in namespace form_for
Asked Answered
P

4

31

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

Persinger answered 27/4, 2010 at 0:56 Comment(0)
T
48

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| %>
Threaten answered 28/4, 2010 at 10:1 Comment(5)
Sure, you will just need to change the url to the update path.Threaten
Which requires that I pass in the url into my form partial... Not a big deal, but it seems that you shouldn't have to do that.Persinger
Hmm, I started messing around with it again and now I have the following that works. form_for [:admin, @person, @image] do |f|Threaten
You should post this as an answer.Catholicism
form_for [:admin, @person, @image] works only if you as well defined un-nested the resources :image, :only => [:show, :update] in addition to your nested namespace :admin do resources :person do resources :image end end. #14613806Englert
S
2

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

Subtemperate answered 18/11, 2013 at 12:58 Comment(0)
H
2

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])
Heathenize answered 3/5, 2014 at 7:54 Comment(2)
I've been after something that fixes the scaffold _form partial like this for ages! Thank you!Schluter
Well, it's effectively the same as writing form_for [:admin, @person, @image], since form_for calls url_for internally.Shippee
H
0

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 %>
Hundred answered 11/8, 2021 at 0:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.