无论我在哪里尝试,我都会收到NoMethodError。转到http://localhost:3000/
会出现undefined method new_movie_rentals_path'
错误。去某个地方,如http://localhost:3000/movies/5/rentals/new
(并且应该有一个ID为5,我还尝试了许多其他数字)给了我一个undefined method 'title' for nil:NilClass
。那么,我相信我遇到了一些路由错误?但我不确定。
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
(电影)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 %>
(出租)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
答案 0 :(得分:0)
您应该尝试使用new_movie_rental_path
您也可以在此处查看约定:http://guides.rubyonrails.org/routing.html
答案 1 :(得分:0)
您致电 new_movie_rentals_path 的地方尝试拨打 new_movie_rental_path
确保在 RentalsController 的新操作中找到了电影