Trip
Trip

Reputation: 27114

What is the proper syntax for setting up this form_for?

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

Upvotes: 2

Views: 106

Answers (1)

zsquare
zsquare

Reputation: 10146

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.

Upvotes: 1

Related Questions