I keep getting a NoMethodError regardless of where I try and go. Going to http://localhost:3000/
gives me an undefined method new_movie_rentals_path'
error. And going somewhere such as http://localhost:3000/movies/5/rentals/new
(and there should be an ID of 5, I also tried many other numbers) gives me an undefined method 'title' for nil:NilClass
. So, I'm getting a few routing errors I believe? But I'm not sure.
movies_controller.rb
class MoviesController < ApplicationController
def new
@movie = Movie.new
@movies = Movie.all
end
def create
@movie = Movie.new(comment_params)
if @movie.save
redirect_to new_movie_path
end
end
def comment_params
params.require(:movie).permit(:title, :year)
end
end
rentals_controller.rb
class RentalsController < ApplicationController
class RentalsController < ApplicationController
def new
@movie = Movie.find(params[:movie_id])
@rental = @movie.rentals.build
end
def create
@movie = Movie.find(params[:movie_id])
@rental = @movie.rentals.build(rental_params)
if @rental.save
redirect_to new_rental_path(@movie)
end
end
def rental_params
params.require(:rental).permit(:id, :borrowed_on, :returned_on, :movie_id)
end
end
end
(movies) new.html.erb
Enter new movie information <br>
<%= form_for @movie do |f| %>
Title: <%= f.text_field :title %> <br>
Year: <%= f.text_field :year %> <br>
<%= f.submit %>
<% end %>
<hr>
List of all movies: <br>
<% if !@movies.blank? %>
<table border=1>
<tr>
<th> Title </th>
<th> Year </th>
</tr>
<% for item in @movies %>
<tr>
<td> <%= link_to new_movie_rentals_path(@movie) %> </td>
<td> <%= item.year %> </td>
</tr>
<% end %>
</table
<% end %>
(rentals) new.html.erb
Movie: <%= @movie.title %> <%= link_to "back", new_movie_path %>
<hr>
<%= form_for [@movie, @rental] do |f| %>
Borrowed on: <%= f.text_field :borrowed_on %> <br>
Returned on: <%= f.text_field :returned_on %> <br>
<%= f.submit %>
<% end %>
Rentals:
<% if !@movie.rentals.blank? %>
<% for item in @movie.rentals %>
<%= item.borrowed_on %>, <%= item.returned_on %> <br>
<% end %>
<% else %>
No rentals yet
<% end %>
routes.rb
Rails.application.routes.draw do
resources :movies do
resources :rentals
end
root 'movies#new'
movie.rb
class Movie < ActiveRecord::Base
has_many :rentals
end
rental.rb
class Rental < ActiveRecord::Base
belongs_to :movie
end
rake routes:
Prefix Verb URI Pattern Controller#Action
movie_rentals GET /movies/:movie_id/rentals(.:format) rentals#index
POST /movies/:movie_id/rentals(.:format) rentals#create
new_movie_rental GET /movies/:movie_id/rentals/new(.:format) rentals#new
edit_movie_rental GET /movies/:movie_id/rentals/:id/edit(.:format) rentals#edit
movie_rental GET /movies/:movie_id/rentals/:id(.:format) rentals#show
PATCH /movies/:movie_id/rentals/:id(.:format) rentals#update
PUT /movies/:movie_id/rentals/:id(.:format) rentals#update
DELETE /movies/:movie_id/rentals/:id(.:format) rentals#destroy
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
new_movie GET /movies/new(.:format) movies#new
edit_movie GET /movies/:id/edit(.:format) movies#edit
movie GET /movies/:id(.:format) movies#show
PATCH /movies/:id(.:format) movies#update
PUT /movies/:id(.:format) movies#update
DELETE /movies/:id(.:format) movies#destroy
root GET / movies#new