我從一個在線教程構建應用程序。它跟蹤「電影」和「出租」。我正在設法創建一個新的租賃部分。當我提交表單,我得到這個錯誤:Ruby on Rails的形成錯誤
ActiveModel::ForbiddenAttributesError in RentalsController#create
以下是完整的租金控制器:
class RentalsController < ApplicationController
def new
@movie = Movie.find(params[:id])
@rental = @movie.rentals.build
end
def create
@movie = Movie.find(params[:id])
@rental = @movie.rentals.build(params[:rental])
if @rental.save
redirect_to new_rental_path(:id => @movie.id)
end
end
end
這似乎再跟這條線具體爲:
@rental = @movie.rentals.build(params[:rental])
這裏是租賃模式:
class Rental < ApplicationRecord
has_one :movie
end
這裏是電影控制器:
class MoviesController < ApplicationController
def new
@movie = Movie.new
@movies = Movie.all
end
def create
@movie = Movie.new(movie_params)
if @movie.save
redirect_to new_movie_path
end
end
private
def movie_params
params.require(:movie).permit(:title, :year)
end
end
這裏是電影模式:
class Movie < ApplicationRecord
has_many :rentals
end
這裏是路線:
Rails.application.routes.draw do
resources :movies, :rentals
root 'movies#new'
end
這裏是形式:
<h1><%= @movie.title %></h1>
<%= form_for @rental, :url => {:action => :create, :id => @movie.id } do |r| %>
Borrowed on: <%= r.text_field :borrowed_on %><br />
Returned on: <%= r.text_field :returned_on %><br />
<br />
<%= r.button :submit %>
<% end %>
<br />
<%= link_to "back", new_movie_path %>
我不知道WH在繼續。從我可以告訴我,我正在複製教程。任何幫助將非常感激!
當您嘗試發送您還沒有加入到PARAMS方法的參數時,該錯誤會發生,在這種情況下,你缺少完全的rental_params方法。 –