Home How to setup form_for with nested routes
Reply: 0

How to setup form_for with nested routes

blu
1#
blu Published in 2017-12-04 20:26:55Z

We have a namespaced model used in a namespaced controller, and an admin namespace inside that.

Routes

namespace: foos do
  namespace: admin do
    resources :bar
  end

  resources: bar, only: [:index, :show]
end

Model

module Foo
  class Bar < ActiveRecord::Base
  end
end

Partial

form_for @bar do
end

This form will not render the correct path; which makes sense.

  • That emits foos_bars_path, not a thing
  • The route is actually foos_admin_bars_path
  • But if we do form_for [:admin, @bar] it emits admin_foos_bars_path

How can we get the path as we have it currently in routes? We are resorting to a url: some_helper_method_to_check_new_record but would prefer a cleaner way. Thanks.

This is for Rails 4.2

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324224 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO