Leonardo Barroeta
Leonardo Barroeta

Reputation: 23

Namespace nested resources

I have the following routes on my project:

namespace :teacher do
 resources job_applications do
   resources :job_application_addresses
 end

My form has the folling code

<%= simple_form_for [:teacher, @job_application_address] do |form|

<% end %>

And my controller has the following:

def new
 @job_application_address = JobApplicationAddress.new
end

def create
 @job_application_address = JobApplicationAddress.new(job_application_address_params)
 @job_application_address.job_application = @job_application
   if @job_application_address.save
    flash[:success] = 'Successfully created'
   end
end

Finally I'm getting this error:

undefined method `teacher_job_application_addresses_path' for #<#<Class:0x00007fda0c4191d0>:0x00007fda143d1af8>
Did you mean?  teacher_job_application_path
               teacher_job_applications_path

Extracted source (around line #3):
<%= simple_form_for [:teacher, @job_application_address] do |form| %>

This are my routes for this view:

teacher_job_application_job_application_addresses GET    /teacher/job_applications/:job_application_id/job_application_addresses(.:format)          teacher/job_application_addresses#index
                                                              POST   /teacher/job_applications/:job_application_id/job_application_addresses(.:format)          teacher/job_application_addresses#create
          new_teacher_job_application_job_application_address GET    /teacher/job_applications/:job_application_id/job_application_addresses/new(.:format)      teacher/job_application_addresses#new
         edit_teacher_job_application_job_application_address GET    /teacher/job_applications/:job_application_id/job_application_addresses/:id/edit(.:format) teacher/job_application_addresses#edit
              teacher_job_application_job_application_address GET    /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format)      teacher/job_application_addresses#show
                                                              PATCH  /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format)      teacher/job_application_addresses#update
                                                              PUT    /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format)      teacher/job_application_addresses#update
                                                              DELETE /teacher/job_applications/:job_application_id/job_application_addresses/:id(.:format)      teacher/job_application_addresses#destroy

What should I do? I'll apprecciate your help.

Upvotes: 2

Views: 80

Answers (1)

SujoyD
SujoyD

Reputation: 159

Your job_application_addresses is nested resource of job_application. But i dont see it in your form. You can do it by 2 ways:

  1. Your can add job_application object in the simple_form_for url builder like this

    <%= simple_form_for [:teacher, @job_application, @job_application_address] do |form|
    
    <% end %>
    
  2. You can add an url option in the form

    <%= simple_form_for [@job_application, @job_application_address], url: teacher_job_application_job_application_addresses_path do |form|
    
    <% end %>`
    

In both cases you need to have @job_application object in new action in controller.

Hope it helps

Upvotes: 2

Related Questions