Undefined method `clinics_path'

I’m trying to create this setup - A user has many clinics.
I have a user table and a clinic table. The clinic table has a user reference.

I’m using devise and wicked wizard to create the user and the first clinic. After the sign up, the user should be able to create more clinics which will be associated to that specific user, and that is what I’m having trouble with. I’ve been struggling with this most of the week, but I can’t figure out what I’m missing or doing wrong, so any help would be much appreciated.

When I click ‘Add new clinic’ on the index.html.erb page I get this error

NoMethodError in Partner::Clinics#new

user.rb

class User < ApplicationRecord

  has_many :clinics, dependent: :destroy
  accepts_nested_attributes_for :clinics, reject_if: :all_blank, allow_destroy: true
  
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

         after_create :send_admin_mail
         def send_admin_mail
           UserMailer.send_welcome_email(self).deliver_later
         end
end

clinic.rb

class Clinic < ApplicationRecord

belongs_to :user

end

Routes.rb

Rails.application.routes.draw do
mount RailsAdmin::Engine => ‘/admin’, as: ‘rails_admin’

devise_for :users, controllers: {:registrations => “users/registrations”
}

resources :registration_steps
root ‘pages#index’
get ‘about’, to: ‘pages#about’
get ‘team’, to: ‘pages#team’
get ‘faqs’, to: ‘pages#faqs’
get ‘faqspractitioners’, to: ‘pages#faqspractitioners’
get ‘faqsusers’, to: ‘pages#faqsusers’
get ‘login’, to: ‘pages#login’
get ‘signup’, to: ‘pages#signup’
get ‘search’, to: ‘pages#search’
get ‘practitioner’, to: ‘pages#practitioner’

get “userprofiles/user_info” => “userprofiles#user_info”, as: “user_info”
get “userprofiles/clinic_info” => “userprofiles#clinic_info”, as: “clinic_info”
get “userprofiles/practitioner_info” => “userprofiles#practitioner_info”, as: “practitioner_info”
get “userprofiles/practitioners” => “userprofiles#practitioners”, as: “practitioners”

patch “userprofiles/user_info” => “userprofiles#update”
patch “userprofiles/clinic_info” => “userprofiles#update”
patch “userprofiles/practitioner_info” => “userprofiles#update”

namespace :partner do
resources :clinics do
resources :practitioners
end
end

devise_scope :user do
scope module: :users do
resources :registrations, only: [] do
member do
delete :delete_image_attachment
end
end
end
end

end

partner/clinics_controller.rb

class Partner::ClinicsController < ApplicationController


def index

@clinic = Clinic.all

@user = current_user

end


def new

@clinic = current_user.clinics.build(params[:clinic])

end


end

Link to the form views/partner/clinics/index.html.erb

            <li class="AddPractitioner">
                <%= link_to [:new, :partner, :clinic], class: 'overlay' do %>
                  <div class="image">
                    <i class="material-icons icon ">add</i>
                  </div>
                <% end %>
                <h4 class="name">Add new practitioner</h4>
              </li>

Form in views/partner/clinics/new.html.erb

              <div id="ClinicGenerel" class="TabBlock">
                <div class="content">
                  <div class="content clinic">
                    <h2 class="page-title">Generel information</h2>
                    <%= simple_form_for [@partner, @clinic] do |f| %>
                      <%= f.simple_fields_for(:clinics) do |p| %>
                        <%= render 'clinics_fields', :f => p %>
                      <% end %>
                      <div class="submit-container">
                        <%= f.submit "Gem", :class => 'btn blue'  %>
                      </div>
                    <% end %>
                  </div>
                </div>
              </div>

Log

Started GET "/partner/clinics/new" for ::1 at 2020-03-08 09:28:09 +0100
Processing by Partner::ClinicsController#new as HTML
  User Load (1.3ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 96 ORDER BY `users`.`id` ASC LIMIT 1
  ↳ app/controllers/partner/clinics_controller.rb:12
  Rendering partner/clinics/new.html.erb within layouts/application
  Rendered partner/clinics/new.html.erb within layouts/application (754.1ms)
Completed 500 Internal Server Error in 766ms (ActiveRecord: 1.3ms)


  
ActionView::Template::Error (undefined method `clinics_path' for #<#<Class:0x00007f9e5b765d58>:0x00007f9e57dcb2a0>
Did you mean?  clinic_info_path):
    44:                     <div class="content">
    45:                       <div class="content clinic">
    46:                         <h2 class="page-title">Generel information</h2>
    47:                         <%= simple_form_for [@partner, @clinic] do |f| %>
    48:                           <%= f.simple_fields_for(:clinics) do |p| %>
    49:                             <%= render 'clinics_fields', :f => p %>
    50:                           <% end %>
  
app/views/partner/clinics/new.html.erb:47:in `_app_views_partner_clinics_new_html_erb__1801308716573597657_70159027799220'

The :clinics route appears to be inside the :partner namespace, so it the route might be named partner_clinics_path instead of just clinics_path.

The way to tell for sure would be to run rake routes (or go to /rails/info/routes on your local server, if I remember rightly); the first column contains the route helper names, which you just need to append _path or _url to to use.

Then you’ll probably need to use the helper in your view: call simple_form_for partner_clinics_path(@partner, @clinic) in views/partner/clinics/new.html.erb instead of the array you’ve currently got.

Then again, simple_form_for may only want the @clinic, so as to set its object correctly – and you’d probably use an option to specify the submission path (something like simple_form_for(@clinic, url: partner_clinics_path(@partner, @clinic)).

Correct me if I’m wrong, I’m going off the top of my head here.

1 Like