Rails Routes Namespaces and form_for

后端 未结 2 773
一向
一向 2020-12-04 07:56

I have namespace in my routes.rb

  namespace :businesses do
    resources :registration
  end

My controller is in a subdirectory businesses

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 08:25

    If you have namespaced routes the best way is:

    class Admin::BusinessesController < ApplicationController
      def new
        @business = Business.new
      end
    end
    

    in routes.rb:

    namespace :admin do
      resources :businesses
    end
    

    In view:

    form_for [:admin, @business] do |f|...
    

    The Docs: http://guides.rubyonrails.org/form_helpers.html section 2.3.1 Dealing with Namespaces

    Regarding your case:

    In routes.rb everything is o'k. In the view you should write url explicitly because you have variable in controller other than controller name:

    form_for @business, :url => business_registration_path do |f|...
    

    I suppose that in businesses/registration_controller you have something like this:

    class Businesses::RegistrationController < ApplicationController
      def new
        @business = Business.new
      end
    end
    

    And one remark: I wouldn't create registration_controller for registering a new business. I think that keeping business related actions in business_controller is much clearer.

提交回复
热议问题