Rails Routes Namespaces and form_for

后端 未结 2 763
一向
一向 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条回答
  • 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.

    0 讨论(0)
  • 2020-12-04 08:39

    Actually, I think there is a better solution.

    form_for [:admin, @business]
    

    the issue with giving a url is that if you abstract the form out as a partial view, you'll need to deal with "create" and "update" situations. They points to different urls, and ends up with providing the @url in controller.

    0 讨论(0)
提交回复
热议问题