Pertanyaan Rute Rail Namespaces dan form_for


Saya memiliki namespace di route.rb saya

  namespace :businesses do
    resources :registration
  end

Kontroler saya ada dalam bisnis subdirektori / registrasi_kontroler.

def new
  @business = Business.new
end

Dalam pandangan saya, saya ingin melakukan ini form_for @business do |f| ... tetapi saya mendapatkan kesalahan berikut:

No route matches {:controller=>"businesses", :action=>"create"}

Restart server dan saya juga mendapatkan ini:

undefined methodbusiness_path 'untuk # <#: 0x10339bb20> `

Inilah rute penggaruk saya:

                   home_index GET    /home/index(.:format)                       {:action=>"index", :controller=>"home"}
             new_user_session GET    /users/sign_in(.:format)                    {:action=>"new", :controller=>"devise/sessions"}
                 user_session POST   /users/sign_in(.:format)                    {:action=>"create", :controller=>"devise/sessions"}
         destroy_user_session GET    /users/sign_out(.:format)                   {:action=>"destroy", :controller=>"devise/sessions"}
                user_password POST   /users/password(.:format)                   {:action=>"create", :controller=>"devise/passwords"}
            new_user_password GET    /users/password/new(.:format)               {:action=>"new", :controller=>"devise/passwords"}
           edit_user_password GET    /users/password/edit(.:format)              {:action=>"edit", :controller=>"devise/passwords"}
                user_password PUT    /users/password(.:format)                   {:action=>"update", :controller=>"devise/passwords"}
     cancel_user_registration GET    /users/cancel(.:format)                     {:action=>"cancel", :controller=>"devise/registrations"}
            user_registration POST   /users(.:format)                            {:action=>"create", :controller=>"devise/registrations"}
        new_user_registration GET    /users/sign_up(.:format)                    {:action=>"new", :controller=>"devise/registrations"}
       edit_user_registration GET    /users/edit(.:format)                       {:action=>"edit", :controller=>"devise/registrations"}
            user_registration PUT    /users(.:format)                            {:action=>"update", :controller=>"devise/registrations"}
            user_registration DELETE /users(.:format)                            {:action=>"destroy", :controller=>"devise/registrations"}
                        users GET    /users(.:format)                            {:action=>"index", :controller=>"users"}
                        users POST   /users(.:format)                            {:action=>"create", :controller=>"users"}
                     new_user GET    /users/new(.:format)                        {:action=>"new", :controller=>"users"}
                    edit_user GET    /users/:id/edit(.:format)                   {:action=>"edit", :controller=>"users"}
                         user GET    /users/:id(.:format)                        {:action=>"show", :controller=>"users"}
                         user PUT    /users/:id(.:format)                        {:action=>"update", :controller=>"users"}
                         user DELETE /users/:id(.:format)                        {:action=>"destroy", :controller=>"users"}
   full_tree_admin_categories GET    /admin/categories/full_tree(.:format)       {:action=>"full_tree", :controller=>"admin/categories"}
             admin_categories GET    /admin/categories(.:format)                 {:action=>"index", :controller=>"admin/categories"}
             admin_categories POST   /admin/categories(.:format)                 {:action=>"create", :controller=>"admin/categories"}
           new_admin_category GET    /admin/categories/new(.:format)             {:action=>"new", :controller=>"admin/categories"}
          edit_admin_category GET    /admin/categories/:id/edit(.:format)        {:action=>"edit", :controller=>"admin/categories"}
               admin_category GET    /admin/categories/:id(.:format)             {:action=>"show", :controller=>"admin/categories"}
               admin_category PUT    /admin/categories/:id(.:format)             {:action=>"update", :controller=>"admin/categories"}
               admin_category DELETE /admin/categories/:id(.:format)             {:action=>"destroy", :controller=>"admin/categories"}
businesses_registration_index GET    /businesses/registration(.:format)          {:action=>"index", :controller=>"businesses/registration"}
businesses_registration_index POST   /businesses/registration(.:format)          {:action=>"create", :controller=>"businesses/registration"}
  new_businesses_registration GET    /businesses/registration/new(.:format)      {:action=>"new", :controller=>"businesses/registration"}
 edit_businesses_registration GET    /businesses/registration/:id/edit(.:format) {:action=>"edit", :controller=>"businesses/registration"}
      businesses_registration GET    /businesses/registration/:id(.:format)      {:action=>"show", :controller=>"businesses/registration"}
      businesses_registration PUT    /businesses/registration/:id(.:format)      {:action=>"update", :controller=>"businesses/registration"}
      businesses_registration DELETE /businesses/registration/:id(.:format)      {:action=>"destroy", :controller=>"businesses/registration"}
                         root        /(.:format)                                 {:action=>"index", :controller=>"home"}

76
2017-10-04 08:05


asal


Jawaban:


Jika Anda memiliki rute yang diberi nama, cara terbaik adalah:

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

di routes.rb:

namespace :admin do
  resources :businesses
end

Dalam penglihatan:

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

Dokumen: http://guides.rubyonrails.org/form_helpers.html bagian 2.3.1 Berurusan dengan Namespaces

Mengenai kasus Anda:

Di routes.rb semuanya o'k. Dalam tampilan Anda harus menulis url secara eksplisit karena Anda memiliki variabel dalam pengontrol selain nama pengontrol:

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

Saya kira dalam bisnis / registration_controller Anda memiliki sesuatu seperti ini:

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

Dan satu komentar: Saya tidak akan membuat registration_controller untuk mendaftarkan bisnis baru. Saya pikir bahwa menjaga tindakan terkait bisnis di business_controller jauh lebih jelas.


163
2017-10-04 08:52



Sebenarnya, saya pikir ada solusi yang lebih baik.

form_for [:admin, @business]

masalah dengan memberikan url adalah jika Anda mengabstraksikan formulir sebagai tampilan parsial, Anda harus berurusan dengan situasi "buat" dan "perbarui". Mereka menunjuk ke url yang berbeda, dan berakhir dengan menyediakan @url dalam pengontrol.


21
2017-12-26 06:12