ruby-on-railsruby-on-rails-4rails-routingform-fornamed-routing

Namespace, form_for and named route


I have two controllers grouped under the admin namespace, like the following:

namespace :admin do
  resources :projects
  get "profile", to: "users#edit"
  post "profile", to: "users#create"
end

In a form_for for a resource, it works fine as eplained in the docs:

form_for [:admin, @project]

But when it comes to a named route, like the above admin/profile, I have no clue what to put as an argument in the form_for. If I simply use form_for(@user), the edit action works fine and the form is displayed correctly, but when submitting I get the following error:

No route matches [PATCH] "/2"

When I try form_for [:admin, @user], the form is not displayed complaing about:

undefined method `admin_user_path'

How should the form_for be constructed in a named route under a namespace?


Solution

  • Try this:

    form_for(@user, url: admin_profile_path) do |f|
    

    Run rake routes to list all routes.