class VenuesController < ApplicationController def create @venue = @user.venues.create!(params[:venue]) render :partial => 'venue_select_box', :success => true, :status => :ok end rescue_from ActiveRecord::RecordInvalid do render :text => 'Put errors in here', :success => false, :status => :unprocessable_entity end end
Using @user.venues in this way, make sure that the user ID is always set accordingly. In addition, ActiveRecord will protect the :user_id field from being assigned during a #create! . Therefore, external attacks cannot change :user_id .
In your tests, you can make sure that executing POST for: create raises an ActiveRecord :: RecordInvalid exception.
FranΓ§ois Beausoleil
source share