2

This is kind of complicated. But I have namespaced routes, and this form is taking care of Customer model that is shared by two different controllers.

my routes:

namespace "self_service" do
  resources :customers

my lousy attempt at instantiated an edit form

= form_for [:self_service, @current_customer], action: 'update', method: :put do |f|

my controller

class SelfService::CustomersController < SelfService::BaseController
  layout 'self_service'

  def edit
  end

  def update
  end
end

This instantiation does 2 things that are wrong :

  1. The url for the form is /customers/146/self_service. But shouldn't it be the other way around? Shouldn't it be self_service/customers/146/ ?

  2. When I click submit, I get a No route matches "/customers/146/self_service"

Update

As it turns out, this.. :

resources :customers do
  member do
    get :self_service

..contradicts this :

  namespace "self_service" do
    resources :customers
  end

But what bothers me here is.. why should they contradict each other? One should be :

customers/:id/self_service

and the other is :

self_service/customers/:id
Trip
  • 26,756
  • 46
  • 158
  • 277
  • possible duplicate of [Rails Routes Namespaces and form_for](http://stackoverflow.com/questions/3853531/rails-routes-namespaces-and-form-for) – Mischa Jun 13 '12 at 14:29
  • I don't think you need to specify the namespace in the form_for – Oscar Del Ben Jun 13 '12 at 14:33
  • 1
    You shouldn't need to specify the update action, does the route change w/out it? – Peter Brown Jun 13 '12 at 15:04
  • Hey Beerlington. no it doesn't. Even bare bones `= form_for [:self_service, @customer] do |f|` – Trip Jun 13 '12 at 15:33
  • That should definitely work, I use it all over the place in my application exactly how you have it. Stupid question, but are you editing the right view? – Peter Brown Jun 13 '12 at 15:55
  • Yes, you need to specify the namespace in the form_for. The only thing that's different from my project is that in my routes.rb i used a symbol not a string to identify the namespace. **:self_service not "self_service"**. – MurifoX Jun 14 '12 at 14:44

1 Answers1

1

The syntax you are using is for nested resources. You dont need to specify the namespace in form_for. Try:

= form_for @current_customer do |f|

-- EDIT --

My mistake. But based on the answer here, it seems what you are doing is correct. Could you try,

= form_for [:self_service, @current_customer] do |f|

and in your routes, use a symbol instead of a string, ie

namespace :self_service do
  resources :customers
end

Not sure if this will work, but worth a shot.

-- EDIT 2 --

Ive also setup a dummy project here with the namespaced resource. I used the rails scaffold generator, and this is what it generated. It creates the form as required. You could follow this as an example.

Community
  • 1
  • 1
zsquare
  • 9,916
  • 6
  • 53
  • 87
  • Where this sets up a proper form, it goes to the wrong controller. I would like this to go to its namespaced controller instead. – Trip Jun 14 '12 at 14:22
  • It was the contradicting routes that were preventing the nested resource from working properly. So I just made one of them into a `match` statement so it specifically said what I wanted it to do. I havn't yet tried out making it a `resources` though. – Trip Jun 15 '12 at 17:46