Home > Software engineering >  I want to do new and create with the associated model
I want to do new and create with the associated model

Time:10-08

What we want to achieve

Use the associated model to new and create. The cause is probably that there is no variable set for @movie. So I would like to know how to get the id of Movie.

Code

routes

# The routes are specified.
Rails.application.routes.draw do
  # get 'movies', to: 'movies#index'
  scope :admin do
    get 'search', to: 'movies#search'
    resources :sheets, only: [:index]
    resources :schedules, only: [:index]
    resources :movies do
      resources :schedules, except: [:index]
    end
  end
end


model

movie.rb

class Movie < ApplicationRecord
  has_many :schedules
end

schedule.rb

class Schedule < ApplicationRecord
    belongs_to :movie
end

controller

#########################################################
# I don't know what's going on here.
  def new
    @schedule = @movie.schedules.build
  end

  def create
    @schedule = @movie.schedules.create(schedule_params)
    redirect_to schedules_path
  end
#########################################################

View

<!DOCTYPE html>
<html lang="en">
  <head>
    <%= render 'shared/head' %>
    <title>schedule/new</title>
  </head>
    <body>
      <%= form_with model: @schedule, url: movie_schedules_path do |form| %>
        <div class="field">
          <%= form.label :start_time %>
          <%= form.date_field :start_time %>
        </div>
        <div class="field">
          <%= form.label :end_time %>  
          <%= form.date_field :end_time %>
          <%= form.submit  %>
        </div>
      <% end %>
    </body>
</html>

What I tried.

@movie = Movie.find(params[:id]) Cannot be obtained due to routes.

web_1  | NoMethodError (undefined method `schedules' for nil:NilClass):
web_1  |   
web_1  | app/controllers/schedules_controller.rb:7:in `new'
web_1  | Started GET "/admin/movies/15/schedules/new" for 192.168.0.1 at 2021-10-05 07:06:39  0000
web_1  | Cannot render console from 192.168.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
web_1  | Processing by SchedulesController#new as HTML
web_1  |   Parameters: {"movie_id"=>"15"}
web_1  | Completed 404 Not Found in 20ms (ActiveRecord: 1.2ms | Allocations: 1755)
web_1  | 
web_1  | 
web_1  |   
web_1  | ActiveRecord::RecordNotFound (Couldn't find Movie without an ID):
web_1  |   
web_1  | app/controllers/schedules_controller.rb:7:in `new'
web_1  | Started GET "/admin/movies/15/schedules/new" for 192.168.0.1 at 2021-10-05 07:06:41  0000
web_1  | Cannot render console from 192.168.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
web_1  | Processing by SchedulesController#new as HTML
web_1  |   Parameters: {"movie_id"=>"15"}
web_1  | Completed 404 Not Found in 7ms (ActiveRecord: 0.0ms | Allocations: 601)
web_1  | 
web_1  | 
web_1  |   
web_1  | ActiveRecord::RecordNotFound (Couldn't find Movie without an ID):
web_1  |   
web_1  | app/controllers/schedules_controller.rb:7:in `new'

enter image description here

CodePudding user response:

You have schedules nested under movies.

Running rake routes | grep movies you will see how the routes look like.

movie_schedules POST            /movies/:movie_id/schedules(.:format)                                                                                  schedules#create
new_movie_schedule GET          /movies/:movie_id/schedules/new(.:format)                                                                              schedules#new
edit_movie_schedule GET         /movies/:movie_id/schedules/:id/edit(.:format)                                                                         schedules#edit
movie_schedule GET              /movies/:movie_id/schedules/:id(.:format)                                                                              
                                                                    

So your parameter is movie_id and doing @movie = Movie.find(params[:movie_id]) will fix the issue.

Here are the docs for Nested Resources.

CodePudding user response:

razvans suggested in a comment that I use

@movie = Movie.find(params[:movie_id]) which worked for me.

  • Related