I am developing the user permissions and I am trying to use a before_action to evaluate a conditional, if it returns false I want the "create" action not to execute, but if it returns true the record will be created
this would be my before_action:
before_action :permiso_create, only: [:create]
def create
@producto = Producto.new(producto_params)
respond_to do |format|
if @producto.save
format.html { redirect_to @producto, notice: 'Producto was successfully created.' }
format.json { render :show, status: :created, location: @producto }
format.js {flash.now[:notice] = 'El producto se ha creado de forma exitosa.'} #ajax
else
format.html { render :new }
format.json { render json: @producto.errors, status: :unprocessable_entity }
format.js {flash.now[:alert] = 'Error al crear el producto.'} #ajax
end
end
end
if the conditional returns true that the record is not created or the create action is not executed, showing a flash alert with the message "permission denied"
I have tried with unprocessable_entity but it does not return the message, stopping the process before showing it, like this:
def permiso_create
@puede_crear = current_usuario.profiles.find_by_Descripcion("products").Altas
if @puede_crear != true
respond_to do |format|
format.js {flash.now[:alert] = 'you don't have permission.'}
end
head :unprocessable_entity
end
end
in what way could I avoid creating and sending that message? thanks in advance.