我有一个UserCourseRegistration表,该表属于User和Course表,而User和Course表通过UserCourseRegistration表为has_many。我的UserCourseRegistration控制器如下所示:
class UserCourseRegistrationsController < ApplicationController
before_action :set_invite, only: [:edit, :show, :update, :destroy]
def index
@invite = UserCourseRegistration.new
end
def create
@invite = UserCourseRegistration.new(invite_params)
if @invite.save
redirect_to '/user_course_registrations'
else
render :new, notice: @invite.errors
end
end
def show
end
def edit
end
def update
if @invite.update(invite_params)
redirect_to @invite
else
render :edit
end
end
def destroy
@invite.destroy
respond_to do |format|
format.json { render json: true }
end
end
private
def set_invite
@invite = UserCourseRegistration.find(params[:id])
end
def invite_params
params.require(:user_course_registration).permit(:course_id, :user_id)
end
end
当我尝试在rspec中对create方法进行控制器测试时,给出以下消息:
失败/错误:发布:创建,参数:{user_course_registration: @user_course_attributes}
ActiveModel :: ForbiddenAttributesError: ActiveModel :: ForbiddenAttributesError
我的测试是:
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
@current_user_admin = create(:admin)
@current_user = create(:user)
sign_in @current_user_admin
end
describe "POST #create" do
context 'user is admin' do
before(:each) do
@school = create(:school, user: @current_user_admin)
@course = create(:course, user: @current_user_admin, school: @school)
@user_course_attributes = attributes_for( :user_course_registration,
user_id: @current_user_admin.id,
course_id: @course.id
)
post :create, params: { user_course_registration: @user_course_attributes }
end
it 'redirect to show' do
expect(response).to have_http_status(302)
expect(response).to redirect_to("/user_course_registration")
end
end
end
那会是什么?